Link to home
Start Free TrialLog in
Avatar of GRChandrashekar
GRChandrashekarFlag for India

asked on

Input Validator

Hello Experts,

I am developing  C# winform application on .NET 4.x framework. I am tired of writing validation in form level as business process keeps changing. What I am looking at one class for each form where in I can write all validations for each controls and just call the validation class to validate all controls with one single error provider. May be an user control for the class.

Not sure how to start off itself :(

Any suggestions ? Please dont suggest application validation block, fluent etc as I have already gone through those and felt it is better write my own validation class
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GRChandrashekar

ASKER

why not develop my own class?

Say i have tbxname, i write a class pass tbxname input to class and in class define whether it should be null or not.

I can pass dteventdate to class and in class define validation for that.

similarly for all contorls. but am not sure how to start off
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nopes I am talking of validation of all forms and each form in diff class. I do agree it may be bad idea but not feasible to write huge code in each form
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Exactly. So what I am requesting you experts is just one example to validate a text box, a list box, a radio button and a combo box so that I can understand how to go about and complete the design which i intent to
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies what I would prefer is loop through all controls in class level by passing parent.control from form to class and write validation for each control in the class. in form level it should just be like validatecontrols(this) which should get true or false from class
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}

This I guess in form level !

What I need is to pass the form to class, in class loop through controls.
Now some how I managed like this.
 public static void ClearControls(Control parent)
        {
            foreach (Control parentctrl in parent.Controls)
            {
                if (ReferenceEquals(parentctrl.GetType(), typeof (GroupBox)))
                {
                    foreach (Control parentgbxctrl in parentctrl.Controls)
                    {
                        if (parentgbxctrl.Visible)
                        {
                            if (ReferenceEquals(parentgbxctrl.GetType(), typeof (TextBox)))
                            {
                                if (parentgbxctrl.Name == "tbxdescription")
                                {
                                    if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text))
                                    {
                                       if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text))
                                       {
                                           
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

This suits my purpose. Now I need to return false after line if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text)) if validation fails and again false after line if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text)) if validation fails. if any one fails i have to return false to form. if both is true i have to return to true to form.
now if the result if false, i need to show error provider in form ( i have only one error provider} with error message for each control if there are multiples. How can I do this? This will solve my problem
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial