GRChandrashekar
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
{
ValidateThisControl(ctl);
}
This I guess in form level !
What I need is to pass the form to class, in class loop through controls.
ASKER
Now some how I managed like this.
public static void ClearControls(Control parent)
{
foreach (Control parentctrl in parent.Controls)
{
if (ReferenceEquals(parentctr l.GetType( ), typeof (GroupBox)))
{
foreach (Control parentgbxctrl in parentctrl.Controls)
{
if (parentgbxctrl.Visible)
{
if (ReferenceEquals(parentgbx ctrl.GetTy pe(), typeof (TextBox)))
{
if (parentgbxctrl.Name == "tbxdescription")
{
if (ValidateRuleClass.Isrequi redstring( parentgbxc trl.Text))
{
if (ValidateRuleClass.Isalpha string(par entgbxctrl .Text))
{
}
}
}
}
}
}
}
}
}
This suits my purpose. Now I need to return false after line if (ValidateRuleClass.Isrequi redstring( parentgbxc trl.Text)) if validation fails and again false after line if (ValidateRuleClass.Isalpha string(par entgbxctrl .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
public static void ClearControls(Control parent)
{
foreach (Control parentctrl in parent.Controls)
{
if (ReferenceEquals(parentctr
{
foreach (Control parentgbxctrl in parentctrl.Controls)
{
if (parentgbxctrl.Visible)
{
if (ReferenceEquals(parentgbx
{
if (parentgbxctrl.Name == "tbxdescription")
{
if (ValidateRuleClass.Isrequi
{
if (ValidateRuleClass.Isalpha
{
}
}
}
}
}
}
}
}
}
This suits my purpose. Now I need to return false after line if (ValidateRuleClass.Isrequi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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