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