Code to enter a preset value into a textbox [duplicate]
Possible Duplicate:
开发者_JAVA百科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)
{
}
}
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.
Well it should be the following code
textBox4.Text = "5";
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)
{
}
}
精彩评论