validate event confusion

G

Guest

Hi all,

I have a simple form with:

1 TextBox
1 Checkbox
1 Button

I have coded the validate events as follows:

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

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Button");
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
MessageBox.Show("Checkbox");
}

For the purpose of this example I always cancel the validating event. But
the behaviour I see suggests the events dont actually get cancelled. ie.
Focus in the textbox, change its content, click the checkbox. The checkbox
becomes checked/unchecked and you get the checkbox message. Do the same then
click the button, you still get the buttons message. However focus doesn't
leavr the textbox???

I though the idea behind setting the cancel event arguements was to stop
further processing until a valid input has been made.

Please advise.

Pete.
 
G

Guest

Further to the above, I removed all validation events from previous example
and coded up the textbox_leave event as follows:

private void textBox1_Leave(object sender, System.EventArgs e)
{
textBox1.Focus();
}

Click the check box - get the checkbox message
Click the button - get the button messsage
Focus remains in the textbox.

I presumed this code would stop focus moving from the textbox.

Pete
 
B

Bruce Wood

You shouldn't need textBox1.Focus() in the Validating event handler.
Simply canceling the event should suffice.

Are you sure that you have CausesValidation = true set on all of the
controls?

BTW, I don't think that anyone completely understands validation. It's
kind of... screwed up, and it's poorly documented on the MSDN site. I
hope that they make improvements in v2.0.
 
G

Guest

Yep, all controls are CausesValidation=true. Just dragged them straight out
of the toolbox and coded up the simple events.

I agree, validation and its current implementation (providing I am not
missing someything really stupid) is a complete waste of time - nice idea bad
implementation. Heres hoping for v2.

Thanks, Pete.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

trinitypete,

Even though I agree that the implementation of the validation in windows
forms is poorly implemented and buggy in some cases, in your particular case
it works as it supposed to. What you need to do is to remove that
textBox1.Focus() call from the evetn handler. Winform's validating prcedure
does that for you, it keeps the focus on the control that fails to validate.
It also doesn't let the form to close. So, if you try to close the form it
will remain open. The problem I believe is that the validation works upon
control's losing and getting the focus. When you set the focus back to the
control losing it it seems to screw up the normal flow of events, thus it
mess up with the control validation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top