Paul Laudeman

Sponsors

The Lounge

News

  • I'm test-driven!
    TestDriven.NET

    MSN Messenger: plaudeman at hotmail dot com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Programmatically invoking the Validating event on Windows Forms controls

As a follow up to my previous post about enhancing the validation controls within Windows Forms, there might be times when you would like to manually invoke the “Validating” event of a control.

For example, you might want to have a routine that fires on the form's “Closing” event or a data save method to loop through all the controls on your form and validate them independently to ensure that all controls are valid. This might be a common scenario in data entry forms where you might create a new record and the user might not touch all the fields on your form (and thus never trigger the “Validating” event of your controls).

There are two different solutions to this problem. One, pointed out by Chris Sells (read more), invokes the Control's “NotifyValidating” event through Reflection. Here is an example:

protected bool IsControlValid(Control controlToValidate)
{
   
Debug.Assert(controlToValidate != null, "Control to validate is null");

    Type t = typeof(Control);

    MethodInfo mi = t.GetMethod("NotifyValidating", BindingFlags.Instance |
    BindingFlags.NonPublic);

    Debug.Assert(mi != null, "Could not get method information.");

    if(((Boolean)mi.Invoke(controlToValidate, null)))
       
return false;
   
else
       
return true;
}

You can also trigger the Validation event by giving focus to a control and taking the focus away. You might even loop through all the controls on your form and focus each of them individually (or an individual control by giving it focus then changing the focus to another control), achieving the same effect:

protected void ValidateForm

   
for(int i = 0; i < this.Controls.Count; i++)
        this.ControlsIdea.Focus();
}

Which solution is better? I'd recommend staying away from reflecting on the internal methods and properties of Framework classes. This will future proof your code as much as possible and prepare you for any breaking changes down the road to the internals of the Framework.

Technorati Tags: ,  


Posted 04-12-2004 7:46 AM by paul.laudeman
Filed under:

[Advertisement]

Comments

Greg Robinson wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 04-12-2004 6:48 AM
We give the control focus to force validating and a push to the bound datasource.
Paul Laudeman wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 04-12-2004 7:05 AM
Right, that's the solution I recommended. I included the other just as an alternate should anyone need it.

Thanks Greg!
Marius wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 07-12-2005 12:58 AM
I would initially agree that the last method is better, but traversing each control and setting focus on and off can have unwanted side-effects. For example, radio buttons will respond to the focus event by changing its checked state. Can anyone tell me how to avoid this behaviour?
Francine Taylor wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 03-29-2006 1:29 PM
Just do a switch on the control type.  That way you can control which types of controls will be focused on.

string sType;
RadioButton rb;
CheckBox cb;
foreach (Control ctl in pcb)
{
sType = ctl.GetType().Name.ToString();

switch (sType)
{
case "RadioButton":
                                       case "TextBox":
                                       ... etc

Bernard Pang wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 10-25-2006 3:45 PM

You can just call the ValidateChildren method on your form. All the controls on the form will be automatically validated (run their Validating events)

Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

       Me.ValidateChildren()

       'save your data...

End Sub

hope it helps.

Bernard Pang

bpang@nevp.com

Aurelio Jaraiz wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 06-14-2007 3:20 PM

Many thanks, Bernard, I think this was the best way to check every control.

Aurelio.

Bernard Pang wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 06-18-2007 5:20 PM

You are welcome, Aurelio.

DC wrote re: Programmatically invoking the Validating event on Windows Forms controls
on 10-01-2007 11:08 PM

But how can we achive in .NETCF as validatechidlren() is missing in NETCF

Add a Comment

(required)  
(optional)
(required)  
Remember Me?