问题
I have a little form with some text boxes and combo boxes, each with their own validating event which just fills the default value either 1 or 0 depending on the box when the user moves on to the next box without entering anything, however i also want to run all the validations when the user just clicks on the submit button directly.
private void Validating_Zero(object sender, CancelEventArgs e)
{
if (((TextBox_Pro)sender).Text == "")
{
((TextBox_Pro)sender).Text = "0";
}
}
private void Validating_One(object sender, CancelEventArgs e)
{
if (((TextBox_Pro)sender).Text == "")
{
((TextBox_Pro)sender).Text = "1";
}
}
private void Start_Validating(object sender, CancelEventArgs e)
{
}
回答1:
Calling ValidateChildren() method of the form, cause all of the child controls of the form to validate their data.
You can also use the the other overload of the method which accepts a validation constraint, ValidateChildren(ValidationConstraints) to limit the validation to immediate child controls, enabled controls, tab stop controls, selectable controls, visible controls or all control.
To see an example of validation using Validating
event and Error
provider control and showing error summary and also read more about validation options in Windows Forms, take a look at these posts:
- Validating user input / Give .NET controls status OK or NOK
- Validation using Validating event and ErrorProvider - Show Error Summary
来源:https://stackoverflow.com/questions/46097796/run-all-validating-events-on-the-form-on-button-click