Step by step to Control Validation:
Add a Form
Go to Form Property window.
Disable AutoValidate.
Add a Text Box in the Form.
Add a Button in the Form.
Verify CausesValidation is true.
The following code add into Validating Event of TextBox and Click event of Button.
Click Event of Button:
TextBox Validating Event:
Add a Form
Go to Form Property window.
Disable AutoValidate.
Add a Text Box in the Form.
Add a Button in the Form.
Verify CausesValidation is true.
The following code add into Validating Event of TextBox and Click event of Button.
Click Event of Button:
if (this.ValidateChildren())
{
MessageBox.Show("Validation succeeded!");
}
else
{
MessageBox.Show("Validation failed.");
}
{
MessageBox.Show("Validation succeeded!");
}
else
{
MessageBox.Show("Validation failed.");
}
TextBox Validating Event:
if (textBox1.Text.Length == 0)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}