By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,263 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,263 IT Pros & Developers. It's quick & easy.

Avoiding validation when closing a window...

P: n/a
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
--

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Tom wrote:
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.

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
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 Lard wrote:

Tom wrote:
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.


--

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.