?
Solved

Input Validator

Posted on 2013-05-22
13
Medium Priority
?
237 Views
Last Modified: 2013-05-30
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
0
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39186792
This might be silly but how can you put ALL the validation code for every possible point of entry into one class ?  (A 10 digit string is different than a date than a currency..... and you say the requirements change).


Maybe you might want to look at developing your own self-validating controls.  For instance you could code a TextBox (based on the standard TextBox) that will only accept integers in a certain range (you supply the range at run time).
0
 

Author Comment

by:GRChandrashekar
ID: 39186996
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
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39187136
I think that is a bad idea or are you talking of just validating the input from one form.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GRChandrashekar
ID: 39187743
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
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39187793
>>and each form in diff class

OK - in your original question it sounded like just one class for all forms.  That is why I criticised it at that point.

What you propose is a rather better design.


I would suggest your validation class would have one function per control, which takes the current contents of that control (not the control itself - separation of logic from GUI) and performs the necessary validation.  Return could be a string with the error message or an empty string if the validation succeeds.
0
 

Author Comment

by:GRChandrashekar
ID: 39187839
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
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39187952
public String MyTextValidator(String inp)
{
  if(inp.Length > 10)
    return "To many chars";
  else
    return "";
}

and so on.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39187959
In the form in some function to check the inputs of all controls:

String s = x.MyTextValidator(txt1.Text);
if(s.Length > 0)
  {
    //Oops - wrong input
    MsgBox.Show(s);
    return false;
   }
.....
//All is OK
return true;
0
 

Author Comment

by:GRChandrashekar
ID: 39188084
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
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 39188098
No problems.  Just loop through the controls in the form
something like
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}
0
 

Author Comment

by:GRChandrashekar
ID: 39188151
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.
0
 

Author Comment

by:GRChandrashekar
ID: 39188209
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
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 total points
ID: 39188860
Like I said earlier - handle the controls in the form and call separate functions to validate the contents of the control.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question