CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Paul Laudeman

Helping You to Make "Smart Clients" Smarter!

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: ,  



Comments

Paul Laudeman said:

Right, that's the solution I recommended. I included the other just as an alternate should anyone need it.

Thanks Greg!
# April 12, 2004 7:05 AM

Marius said:

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?
# July 12, 2005 12:58 AM

Francine Taylor said:

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

# March 29, 2006 1:29 PM

Bernard Pang said:

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

# October 25, 2006 3:45 PM

Aurelio Jaraiz said:

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

Aurelio.

# June 14, 2007 3:20 PM

Bernard Pang said:

You are welcome, Aurelio.

# June 18, 2007 5:20 PM

DC said:

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

# October 1, 2007 11:08 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors