Avoiding validation when closing a window...

T

Tom

How is the best way to avoid validation when closing a
window? For instance, I have a Windows Forms window which
has a validation event for a text box. However, if one
enters invalid data in then and then attempts to close the
window (either via my custom 'Close' box or by clicking the
close 'X' in the upper right window corner), the validation
event still triggers and it tells the user that they have
invalid data. Which of course means they need to clear that
data out then close the window.

Sure, I could trap the close event (both in my custom CLOSE
box and also via WndProc), set a form level 'Closing'
variable and check that in my validate code, but there has
GOT to be an easier way. Why does Windows insist on firing
the validate event when a window is closing?

Is there no easier way to avoid the Validate event when
closing? Thanks.

Tom
--
 
L

Larry Lard

Tom said:
How is the best way to avoid validation when closing a
window? For instance, I have a Windows Forms window which
has a validation event for a text box. However, if one
enters invalid data in then and then attempts to close the
window (either via my custom 'Close' box or by clicking the
close 'X' in the upper right window corner), the validation
event still triggers and it tells the user that they have
invalid data. Which of course means they need to clear that
data out then close the window.

Sure, I could trap the close event (both in my custom CLOSE
box and also via WndProc), set a form level 'Closing'
variable and check that in my validate code, but there has
GOT to be an easier way. Why does Windows insist on firing
the validate event when a window is closing?

Is there no easier way to avoid the Validate event when
closing? Thanks.
From the help:

Closing The Form and Overriding Validation
A side effect of the control maintaining focus when data is invalid is
that it is impossible to close the parent form in any of the usual ways
you close a form:

* Clicking the Close box
* Via the System menu that appears when you right-click the title bar
* Calling the Close method programmatically
However, in some cases, you might want to allow the user to close the
form regardless of whether the values in the controls are valid. You
can override validation and close a form that still contains invalid
data by creating a handler for the form's Closing event. In the event,
set the Cancel property to False. This forces the form to close.

Note If you force the form to close in this way, any information in
the controls that has not already been saved is lost.
Note Modal forms do not validate the contents of controls when
closed. You can still use control validation to lock focus to a
control, but you do not need to be concerned with the behavior with
regard to closing the form.
 
T

Tom

Larry: Thanks for the info... This works, yet it doesn't
work. Yes, it does cause the form the end even with invalid
data in a field; however the validation routine still runs
so you still get an error message (then the form closes).
Again, although this works it is still ugly. I would rather
just have it close the window and NOT give any error
routine.

Too bad there isn't any way to actually look to see if a
window is closing from the validation routine.

Tom


Larry said:
Closing The Form and Overriding Validation
A side effect of the control maintaining focus when data
is invalid is that it is impossible to close the parent
form in any of the usual ways you close a form:

* Clicking the Close box
* Via the System menu that appears when you right-click
the title bar * Calling the Close method programmatically
However, in some cases, you might want to allow the user
to close the form regardless of whether the values in the
controls are valid. You can override validation and close
a form that still contains invalid data by creating a
handler for the form's Closing event. In the event, set
the Cancel property to False. This forces the form to
close.

Note If you force the form to close in this way, any
information in the controls that has not already been
saved is lost. Note Modal forms do not validate the
contents of controls when closed. You can still use
control validation to lock focus to a control, but you do
not need to be concerned with the behavior with regard to
closing the form.



--
 

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