开发者

.net WinForms control validation - how to explicitly validate all controls

开发者 https://www.devze.com 2023-03-11 16:10 出处:网络
If I create a simple Winforms app with a button and a textbox, and the following event handlers, I\'d expect to see \"False\" when I hit the button.When I hit the button, it actu开发者_运维百科ally pr

If I create a simple Winforms app with a button and a textbox, and the following event handlers, I'd expect to see "False" when I hit the button. When I hit the button, it actu开发者_运维百科ally produces "True".

Why is the form valid? It doesn't appear that the validating event is executing at all, even though the docs say that passing false causes validation to be performed unconditionally.

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.Validate(false).ToString());
    }

    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        e.Cancel = true;
    }


It looks like you're trying to validate a child control of the Form. If that's the case, you should use one of the ValidateChildren methods instead of Validate.

0

精彩评论

暂无评论...
验证码 换一张
取 消