Validation of all controls on a Windows form

G

Guest

All,
I have a number of controls on a form. Each has its'
own "Validating" event handler in order to make sure the
user enters "valid" data (this can also be used to make
sure the user enters required fields...the data is
initially null/empty, etc..)

Everything works fine if the user attempts to tab away
from a control..the validating event fires and the
appropriate routines validate the user entered data.

HOWEVER, if the user directly presses the "done" button,
closes the form, or "skips" controls (using the mouse)
problems arise...basically I canNOT figure out how to
validate all the controls when the form is
submitted/closed. I would like to do something like this:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
System.ComponentModel.CancelEventArgs e1;
foreach (Control c in this.Controls)
{
// signal the validating event for control c
// and CANCEL the close Event for the form if the
// control returns e.cancel = true in its' event args
}
}

the problem is I canNOT figure out how to directly call
the controls validating event. I realize I could directly
call my validation routine for each control, however that
would be a real maintenance headache since you would have
to remember to add all future validation routines for any
new controls to the FORM LEVEL validation routine....it
would really be nice to iterate through all the controls
and do the work in one place (i.e. in the controls'
validation event...)

Am I missing something? The real issue deals
with "required" fields and the fact the user could
still "submit" this form (using this event validation
approach) without entering data (unless form level
verification was added)

Thanks,
Fred Iannon
 
D

Dan

You probably already tried this, but
try setting CausesValidation on your button to true. I
think that this should raise all of the validating events
on your control. Also make sure you are setting e.Cancel
= true in the validating events of your controls if they
are invalid.
 
G

Gary

Hi,

I just put code such as
bool dosearch=true;

if (txtSurname.Text=="")
{
this.errProvCustomerSearch.SetError
txtSurname,"Mandatory");
dosearch=false;
}
if (txtForename.Text=="")
{
this.errProvCustomerSearch.SetError
(txtForename,"Mandatory");
dosearch=false;
}
if (dosearch)
{
//Search and close form
}

It IS a maintenance headache, but if you're concerned
about mandatory data rather then strings in an int field
for example, then I think it's the only way to go.

I don't use the validating event to deal with any
mandatory fields as you just end up not letting the user
leave the control, which is pretty horrible.

Gary
 
J

Joe White

basically I canNOT figure out how to
validate all the controls when the form is
submitted/closed. I would like to do something like this: ....
// signal the validating event for control c
// and CANCEL the close Event for the form if the
// control returns e.cancel = true in its' event args

Unfortunately, that's not the way WinForms validation is designed. It's
designed to validate only one control at a time (the currently-focused
control, when it loses focus). The general idea seems to be "don't let
the user type alpha characters into a numeric edit field", while
completely ignoring the "make sure the user entered SOMETHING into each
field" case.

I've actually spent the past few days looking through the validation
code in Reflector, and there really isn't a way to call a control's
Validating event directly (without using reflection or something of the
sort). You'll have to do something manually, e.g., making an ArrayList
and adding a bunch of delegates to it, then iterating through them and
calling each one (and stopping as soon as you get to one that sets
e.Cancel = true). If you also want to use the WinForms Validating event
for each control, then you'll have to build this system in parallel with
it, i.e., you'll have to add each Validating event to both the control
and the ArrayList.

Sorry there isn't an easier answer. WinForms validation is good for the
things it was designed for, but it just wasn't designed for validating
an entire screen of information.
 
C

codymanix

you could subclass the controls and so you can call OnValidating directly
since it has protected access.

Additionally you dont have to validate all controls on the form but only the
one that is currently focused since all others are already validated after
you left them.
 
C

codymanix

Dan said:
You probably already tried this, but
try setting CausesValidation on your button to true. I
think that this should raise all of the validating events
on your control. Also make sure you are setting e.Cancel
= true in the validating events of your controls if they
are invalid.

And what if I have a keyshortcut which causes the data on the form to be
saved?
 

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