For Programmers: Free Programming Magazines  


Home > Archive > C# > October 2005 > validation in windows forms (C#)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author validation in windows forms (C#)
mishra712@gmail.com

2005-10-06, 3:56 am

Hi All,


I have a few text boxes on my WinForm which need to be validated before

they lose focus. For this I have used 'validating' event, which shows a

message box when the validation fails. What I want is that this
validation shouldn't take place when i click on the exit button. I have

set the 'Causes Validation' property of the Exitbutton as false, still
when I click on it the validating event gets fired.


Does anybody has solution to this?
Thanks in advance.


Abhishek

Bruce Wood

2005-10-06, 6:59 pm

Validation in Windows Forms is an absolute horror. I spent a donkey's
age getting something that works, although I'm sure it's not the
approved Microsoft way. I came across one Web page that went into gory
detail about exactly why the whole Validating / CausesValidation thing
is broken, written by a guy who did a lot of investigation and became
thoroughly frustrated with the whole mess.

Although you didn't specify, I'm guessing that you're seeing the
following behaviour: setting CausesValidation to false on your
ExitButton at least gets you into the "Clicked" event handler of the
button, but then, sometime after that, when your form is closing, the
validation events are raised. Is that what you're seeing?

If so, I think I know why: CausesValidation = false does not _get rid_
of the validation events. You can't get rid of them. All it does is
_delay_ them long enough to run the "Clicked" event handler of the
button. What I've seen on forms is that tabbing over controls that have
CausesValidation = false stops the Validating events from being raised,
but then as soon as you tab onto a control with CausesValidation =
true, the Validating event from that validating control "way back
there" in the tab order is suddenly raised. It's like the Validating
event is in a queue somewhere, waiting for you to land on something
with CausesValidation = true so it can "fire".

So, the first order of business is to set CausesValidation = false for
all layout controls on your form. All of your group boxes, panels, and
the form itself should have CausesValidation = false. Yes, I know that
panels don't receive focus... don't ask. Somehow it makes a difference.
(I told you that this stuff was broken, didn't I? :)

The second order of business is to create a bool flag. Let's call it
"_cancelValidation", just for the sake of this discussion. Now, every
....Validating() event handler that you have should look like this:

private void ...Validating(...)
{
if (!this._cancelValidation)
{
... do your validation code here...
}
}

this allows you to set "cancelValidation" to true, and any
....Validating events that are raised will still fire, but you'll ignore
them. Now, in your ExitButton_Clicked handler, just say:

private void ExitButton_Clicked(...)
{
this._cancelValidation = true;
...
}

now, if any ...Validating() events are raised, they'll be ignored by
your code. You can also use this if you have to repopulate controls in
order to give the user a way to reset form values and not get stuck in
a validating control forever. In this case, however, you have to
_force_ validation in order to get rid of any pending Validating
events, otherwise you'll reset your flag to false too soon and the user
will still be stuck.

Yes, having a little boolean flag around is hokey, but it works.

I really wish that Microsoft would supply a CancelPendingValidation()
method or something to get rid of those "pending" validation events, or
publish some sort of white paper with a non-trivial example of how this
stuff is supposed to work. No, it doesn't work the way they claim it
does. In particular, CausesValidation = false appears to not only
affect validation of the preceding control (as advertised), but also,
in some situations, appears to affect validation of the control itself
(the opposite of what MS claims). It's all a confusing mess, as I said,
and one hokey little boolean flag seemed a small price to pay to
maintain my sanity.

Good luck.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com