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
.
精彩评论