在单击按钮的窗体上运行所有验证事件 [英] Run all Validating events on the Form on button click

查看:54
本文介绍了在单击按钮的窗体上运行所有验证事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些文本框和组合框的小表格,每个框都有自己的验证事件,当用户移至下一个框而不输入任何内容时,它们仅填充默认值1或0(取决于框)我也想在用户直接单击提交"按钮时运行所有验证.

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)
{

}

推荐答案

调用 ValidateChildren() 方法,使该表单的所有子控件验证其数据.

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.

要查看使用 Validating 事件和 Error 提供程序控件进行验证的示例,并显示错误摘要,并阅读有关Windows Forms中验证选项的更多信息,请查看以下内容帖子:

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:

这篇关于在单击按钮的窗体上运行所有验证事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆