[ACCEPTED]-C# - TextBox Validation-validation
There are several events that you can use 4 here, Leave
, LostFocus
and Validating
there is more discussion of 3 these various events on MSDN here.
Under certain scenarios 2 the Leave
and the LostFocus
will not fire so the best 1 to use in your case is the Validating
event:
textBox1.Validating += new CancelEventHandler(textBox1_Validating);
void textBox1_Validating(object sender, CancelEventArgs e)
{
int numberEntered;
if (int.TryParse(textBox1.Text, out numberEntered))
{
if (numberEntered < 1 || numberEntered > 10)
{
MessageBox.Show("You have to enter a number between 1 and 10");
textBox1.Text = 5.ToString();
}
}
else
{
MessageBox.Show("You need to enter an integer");
textBox1.Text = 5.ToString();
}
}
if you are hand-rolling validation like 7 you do here, all you need to do is to set 6 the default value after you MessageBox.Show()
in 5 standard winforms I don't think you have 4 any framework support for validation, but 3 you could look at this: http://msdn.microsoft.com/en-us/library/ms951078.aspx for inspiration 2 so you don't scatter this logic throughout 1 your app
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.