Avoiding that closing the Form fires Validating event

C

cody

I have a form and I want to avoid that if the user clicks on the close
button, that the validating event of the currently active control is called.
This is because normally if the user enters a wrong value and tries to leave
the textbox a messagebox pops up.
But if the user wants to close the form no validating should occur because
the user must be able to leave the form at every time without correcting any
value on the form.
 
H

Hans de Vetten

Hi Cody,

The button has a property 'CausesValidation'. Simply set this to false and
it will prevent any validation from happening.

Hans de Vetten
 
C

cody

The X-Button on top of every Form is not a real Button, therefore it has no
Causesvalidation event.
I tried to make CausesValidation for the Form to be false but it didn't
helped either.
I also tried to set a flag if OnValidating,OnLeave,OnClosing of the form is
called and do not validate in that case, but it seems that the validation of
the textbox occures *before* Iam able to set the flag.
 
B

Burkhard Perkens-Golomb

I have a form and I want to avoid that if the user clicks on the
close button, that the validating event of the currently active
control is called. This is because normally if the user enters a
wrong value and tries to leave the textbox a messagebox pops up.
But if the user wants to close the form no validating should occur
because the user must be able to leave the form at every time
without correcting any value on the form.

If I remember right you can't prevent throwing the validating event of
the textbox. A solution but with a more fundamental change: use
ErrorProvider instead of message boxes to inform the users of wrong
values.

Burkhard
 
C

cody

I have a form and I want to avoid that if the user clicks on the
If I remember right you can't prevent throwing the validating event of
the textbox. A solution but with a more fundamental change: use
ErrorProvider instead of message boxes to inform the users of wrong
values.


But I need to make sure that the user cannot leave the Textbox until he
enters a correct value.
 
B

Burkhard Perkens-Golomb

I have a form and I want to avoid that if the user clicks on the
But I need to make sure that the user cannot leave the Textbox until
he enters a correct value.

Huh? But before you said:
the user must be able to leave the form at every time
without correcting any value on the form.

ErrorProvider has nothing to do with the ability to leave the Textbox
or not, it just displays an icon next to the control with the error.
If you want to prevent leaving a control, attach to the
Validating event of the control and set the cancel flag of the
CancelEventArgs accordingly.

Burkhard
 
C

cody

I have a form and I want to avoid that if the user clicks on the
Huh? But before you said:


ErrorProvider has nothing to do with the ability to leave the Textbox
or not, it just displays an icon next to the control with the error.
If you want to prevent leaving a control, attach to the
Validating event of the control and set the cancel flag of the
CancelEventArgs accordingly.


That exactly is the problem: I *use* the validating event to prevent the
user from leaving the *TextBox* but the user must be able to leave/close the
*form* without triggering the validation event! The problem is that if the
user tries to leave the form or clicks the x-button, the validating event
fires although it shouldn't.

I do not want to use Errorprovider instead of MessageBox because the
errorprovider does not inform the user properly what exactly was wrong (yes
you can click on the exclamation mark but not every user knows that).
Additionally it is the best way for my app to inform the user instantly that
a value was wrong, using an errorprovider would mean that the user could
notice the error eventually 10 TextBoxes later and has to go back first.
 
B

Burkhard Perkens-Golomb

On 18 Okt 2004, (e-mail address removed) wrote:

[...]
That exactly is the problem: I *use* the validating event to prevent
the user from leaving the *TextBox* but the user must be able to
leave/close the *form* without triggering the validation event! The
problem is that if the user tries to leave the form or clicks the
x-button, the validating event fires although it shouldn't.

I see.
I do not want to use Errorprovider instead of MessageBox because the
errorprovider does not inform the user properly what exactly was
wrong (yes you can click on the exclamation mark but not every user
knows that).

Users of my current project like the ErrorProvider much more than
message boxes :) .
Additionally it is the best way for my app to inform the user
instantly that a value was wrong, using an errorprovider would mean
that the user could notice the error eventually 10 TextBoxes later
and has to go back first.

If your application does cross checks between two or more values your
user has to go back anyway.

But back to your problem: Sorry, don't know a simple solution, but
there might be a workaround: After clicking the close button and
before the TextBox raises the Validating event, the method
"Form.WndProc" is called with a WM_CLOSE message (i.e., the parameter
of the method "Form.WndProc" is a System.Windows.Forms.Message with
the property "Msg" returning 0x10). To see this set a breakpoint in
the TextBox.Validating handler and look at the callstack (activate
"Show non-user code" in the callstack window of VS!).

So you can override "Form.WndProc", set a flag if the parameter is a
WM_CLOSE message and test for the flag in your handler of the
TextBox.Validating event (or override TextBox.OnValidating and
suppress the event if the flag is set or ...).

Not very simple nor elegant, but should work.

I hope the user clicking the close button can't cancel closing later
on. Otherwise you have an unchecked wrong value in a textbox :->.

Burkhard
 
C

cody

there might be a workaround: After clicking the close button and
before the TextBox raises the Validating event, the method
"Form.WndProc" is called with a WM_CLOSE message (i.e., the parameter
of the method "Form.WndProc" is a System.Windows.Forms.Message with
the property "Msg" returning 0x10). To see this set a breakpoint in
the TextBox.Validating handler and look at the callstack (activate
"Show non-user code" in the callstack window of VS!).

So you can override "Form.WndProc", set a flag if the parameter is a
WM_CLOSE message and test for the flag in your handler of the
TextBox.Validating event (or override TextBox.OnValidating and
suppress the event if the flag is set or ...).


Thank you I'll try it.
 

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