Control's Validating Event and closing a Form

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

We handle the Validating event for textboxes that are contained in a
GroupBox, which is contained in a TabControl which is contained in a Form.
If Validation on the textbox fails, we do this:
e.Cancel = True
Now, when we click "X' in the upper right corner, the form closes. We
checked the forms closing event, and now e.Cancel = False.
Why is e.Cancel = False on the Form close?
 
Greg,

The form object is a different sender then your textbox object. It used the
same type of event args, but it is not the same instance. The values will
be different.

I think that is what you were looking for... if not, sorry.

Thanks for the reply on the No touch by the way. I understand about the
Sand Box and the CAS tool. I wanted to avoid an extra install by the client
was all.

Thanks
 
Why does the form close then when validation fails on the textbox and
e.cancel is set to True?
 
Inside the Form Closing event method if you set the e.Cancel to true, it
should not allow the form to close. Setting the e.Cancel to true in the
Texbox validating event just stops the event message pipeline so that
Validated and Lostfocus are never reached in the event pipeline. Allowing
you to display a message and such.

Am I making sense or just confusing you more?

Hope I am helping...


Greg said:
Why does the form close then when validation fails on the textbox and
e.cancel is set to True?

"Ben S. Stahlhood II" <ben[.dot.]stahlhood[.at.]intellified[.dot.]com> wrote
in message news:[email protected]...
Greg,

The form object is a different sender then your textbox object. It used the
same type of event args, but it is not the same instance. The values will
be different.

I think that is what you were looking for... if not, sorry.

Thanks for the reply on the No touch by the way. I understand about the
Sand Box and the CAS tool. I wanted to avoid an extra install by the client
was all.

Thanks
 
In the textbox's Validating event, we set e.cancel to True. Next, I
click the form's 'X' and the form closes. Why? This should not happen
in my opinion. The form should not be allowed to close.
 
Beacause the CancelEventArgs instance for that method are only for the
validation of the Textbox and the event pipeline for the Textbox control.
The form event arguments has nothing to do with it, you should set a flag
and check it in the Form "Closing" event and set the e.Cancel = true so that
the form can not close.
 
I disagree with this, somewhat. Try it out. Create a form, drop a textbox
on it. Handle the Validating event for the textbox. If it's text is empty,
set e.cancel to true. Now try to close the form, you cannot.

This works in a demo, but not in our application.
 
Greg,

I am not in any way trying to insult your intelligence here, but have you
checked in the designer to make sure the event is hooked to the event
handler method you speak of? Cause if you cut the textbox for example and
pasted into the group box, it will remove the bindind for the event hanlder.
You have to actually goto the textbox control events page and choose the
method again from the combobox. I am just trying to duplicate the steps you
took and help out the best I can...

Thanks

Ben S. Stahlhood II


Greg said:
I disagree with this, somewhat. Try it out. Create a form, drop a textbox
on it. Handle the Validating event for the textbox. If it's text is empty,
set e.cancel to true. Now try to close the form, you cannot.

This works in a demo, but not in our application.

"Ben S. Stahlhood II" <ben[.dot.]stahlhood[.at.]intellified[.dot.]com> wrote
in message news:%[email protected]...
Beacause the CancelEventArgs instance for that method are only for the
validation of the Textbox and the event pipeline for the Textbox control.
The form event arguments has nothing to do with it, you should set a flag
and check it in the Form "Closing" event and set the e.Cancel = true so that
the form can not close.
 
Back
Top