Ok, so I'm just messing around with c# at the moment, learning some stuff -- it is surprisingly similar to JavaScript :S -- and I hit a snag: I'm grabbing the value of a texbox and then setting the textbox to have zilch in it. Consider the below code (lines 38 - 42)
However, when I check the value of Name it's empty. I know this is because I'm setting the nameInput (textbox) to empty, so is there a way I can hold on to the value?
PS: Anyone find it annoying that line numbers are disabled by default?! >=[
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string Name;
string Nameref;
public Form1()
{
InitializeComponent();
}
private void setName_Click(object sender, EventArgs e)
{
// set the value from nameInput to Name
// we shall check for a value first
int NameLength = nameInput.Text.Length;
Regex Pattern = new Regex( "[^a-zA-Z]" );
if ( NameLength < 1 )
{
MessageBox.Show( "Please enter a name", "Error" );
}
else if( Pattern.IsMatch( nameInput.Text ) )
{
MessageBox.Show( "Please enter only letters", "Error" );
}
else
{
Name = nameInput.Text;
nameInput.Text = "";
this.Text = "Name set";
}
}
private void nameReminder_Click(object sender, EventArgs e)
{
// check if Name is set
int NameLength = nameInput.Text.Length;
if( NameLength < 1 )
{
MessageBox.Show( "Name has not been set yet. Please do so now", "Error" );
nameInput.Focus();
}
else
{
MessageBox.Show( Name, "Your name" );
}
}
}
PS: Anyone find it annoying that line numbers are disabled by default?! >=[
Comment