Run all Validating events on the Form on button click

孤者浪人 提交于 2020-01-02 19:29:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!