PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
validate event confusion
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
validate event confusion
![]() |
validate event confusion |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 "trinitypete" wrote: > 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. > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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. |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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. "Bruce Wood" wrote: > 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. > > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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. -- Stoitcho Goutsev (100) [C# MVP] "trinitypete" <trinitypete@online.nospam> wrote in message news:27DD6CB6-630E-45D0-BE5F-DA14D549658F@microsoft.com... > 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. > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

