0

Possible Duplicate:
C# - TextBox Validation

I have an if statement, and if true I would like it to restore a default value of a text box (5). Could someone demonstrate how you can enter a predefined (5) value into a text box from a method such as:

private void textBox4_Leave(object sender, EventArgs e)
{
     try
     {
         int numberEntered = int.Parse(textBox4.Text);
         if (numberEntered < 1 || numberEntered > 28)
         {
              // Code to restore value of textbox here
         }
     }
     catch (FormatException)
     {
     }
} 
Community
  • 1
  • 1
Jay
  • 185
  • 2
  • 2
  • 5

3 Answers3

0

Well it should be the following code

textBox4.Text = "5";
Zeus
  • 123
  • 1
  • 6
0

Textbox.Text is both a getter and a setter. Just assign the value.

Other comment: textBox4 is a terrible variable name. You should give it a name that conveys what it is being used for.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0

Save the predefined value somewhere:

readonly string  TEXTBOX_PREDEFINED_VALUE = "Foo!";

private void textBox4_Leave(object sender, EventArgs e)
        {

            try
            {
                int numberEntered = int.Parse(textBox4.Text);
                if (numberEntered < 1 || numberEntered > 28)
                {

                   textBox4.Text = TEXTBOX_PREDEFINED_VALUE;

                }
            }
            catch (FormatException)
            {


            }
        } 
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185