Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
my friend have added a usercontrol on a panel, and usercontrol consist of comboboxes and textboxes,
so he want to validate that
i used this method for simple validation
public void validation()
        {
           
            foreach (Control ctrl in panel1.Controls)
            {
                UserControl1 myCtrl = ctrl as UserControl1;
                if (ctrl is UserControl1)
                {
                    
                    if (myCtrl.comboBox1.Text == string.Empty)
                    {
                        label3.Visible = true;
                    } 
                    else
                    {
                        label3.Visible = false;
			form1.show();

                        
                    }
                }

            }

but this code is only for one validation,i want more validation here for example i want all the textbox and comboboxes
to not to be left blank or empty, so how to combine the code
previously i used two methods like this
C#
private bool valpass()
        {
            if (textBox2.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
         private bool valcombo()
        {
            if (comboBox1.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

and then in the button click event i used such type like
C#
bool val = valpass();
  bool  valcom =valcombo();
if (!val && !valcombo)
{
//then do this
}
else
{
//do that
}


but i dont know how to iterate here, as usercontrol is added dnamically to the panel and i want to validate it outside
as the panel as my submit button is outside the panel.
Posted

Iteration in the control is simple if you use recursion:

C#
delegate bool Validator(Control control);

//...

static bool Validate(Control control, Validator validator) {
    bool isValid = validator(control);
    foreach (Control child in control.Controls)
        isValid = isValid && Validate(child, validator);
    return isValid;
}


Consider you have the implementation of Validator, a delegate instance capable of doing validation for one individual control instance. It does not have to be a static delegate instance, because it may need access to some instance like a form. The recursive method will iterate all controls. You can add additional parameters to the delegate, to pass data or something.

One problem of this approach is that the validator is agnostic to the run-time type of the control, so, you would need to do the dynamic cast via the as operator and do different validation for different control type. This is a certain abuse of "pure" OOP. As there are pieces of programming where such things are unavoidable, I provided some very general solution it it. Please see my CodeProject article: Dynamic Method Dispatcher[^].

Another solution is more regular OOP. Subclass all of your control classes to implement some common interface, such as IValidate and use the interface reference in the validator instead Control.

Those are the solutions for Boolean validation.

And finally, you can use System.Windows.Forms approach: handle the events Validating and Validated in all your control; don't forget to set CausesValidation. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx[^].

—SA
 
Share this answer
 
Comments
sariqkhan 17-Dec-12 0:34am    
sir, this one is complicated, do you have loops, it easy to understand than delegates,
Sergey Alexandrovich Kryukov 17-Dec-12 0:39am    
It is not. Loops are not alternatives to delegates. A delegate is just an abstraction of a method. Besides, you don't really work without delegates, even now, because you already use events; so you better know how they work. You can use one hard-coded method instead, but you loose flexibility. Why won't you just try?

And pay attention for the last alternative — a Forms way. This way already validates every child, only there is no one Boolean result.

—SA
sariqkhan 17-Dec-12 0:43am    
have you gone through my code,
i used a very simple example to validate the control, i can used that method in the leave event also,for validation, there is no other alternative to used delegates here in this case?
sariqkhan 17-Dec-12 0:37am    
if i will impliment this,so how to use this as i am a newbie to programming i dont know to delegate concept,
if you can tell how to validate a combo and text with your given examle
Sergey Alexandrovich Kryukov 17-Dec-12 0:42am    
In one of my articles, the one I referenced above, there is a detailed explanation of delegates, in the section "4.1. On the Nature of Delegate Instance". You need to know them anyway, just think about it. However, this is not my job to convince you — your work will force you to learn this stuff, anyway...
—SA
If It was me I will be doing like this.

Add a Public method inside the UserControl say
C#
public bool ValidateControl()
{
  bool success = false;
  do
  {
     //Validate TextBox
     if(!ValidateControl(textBox))
     {
       break;
     }
     //Validate ComboBox   
     if(!ValidateControl(comboBox))
     {
       break;
     }
     //like the above you can add any further validation if no
     //other validations are there
      success = true;
  }while(false);
   
   //Validate Anything you want
  return success;
}

bool ValidateControl(TextBox textBox)
{
//your textbox validation code goes here
return true/false;
}

bool ValidateControl(ComboBox combo)
{
//your combobox validate code goes here
return true/false;
}
/EDIT - code sample modified as OPs comment:

let me know of you want more. just think about the top level flow of the program and drilldown to each of the child control for a better design of your application.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900