1

I am developing a Windows Form Application which has lots of Forms and each Form has several controls. In order to implement Validation I have to implement "Validating" event for each controls as following:

    private void txtSalary_Validating(object sender, CancelEventArgs e)
    {
        if (txtSalary.Text.Trim() == String.Empty)
        {
            errorProvider1.SetError(txtSalary, "Salary is Required");
            e.Cancel = true;
        }
        else
            errorProvider1.SetError(txtSalary, "");

    }

Is there any way by which i can do something generic? Rather than declaratively defining the "Validating" event for each control(Form Design View -in controls properties) could i associate the event with the controls in a generic way through code?

techmad
  • 933
  • 2
  • 11
  • 14

3 Answers3

0

You can use only one event for each form, as an example you could use something like this

private void textboxValidation_Validating(object sender, CancelEventArgs e)
{
    TextBox tb = sender as TextBox;

    if (string.IsNullOrEmpty(tb.Text.Trim()))
    {
        errorProvider1.SetError(tb, tb.Tag +" is Required");
        e.Cancel = true;
    }
    else
        errorProvider1.SetError(tb, "");
}

This could be used for all the textboxes in the form, you just need to add what would be required by each textbox in its Tag property.

It is also possible to use one event for all forms. Take a look at Set up single event handler for multiple forms

Community
  • 1
  • 1
coolmine
  • 4,427
  • 2
  • 33
  • 45
  • Thanks. But where How I can associate textboxValidation_Validating event for all controls? Moreover I want to hook the event with all Controls using Code – techmad Mar 22 '13 at 10:44
  • To programatically associate the event to the textboxes use textBox1.Validating += new CancelEventHandler(textboxValidation_Validating); Where textBox1 will be the name of the textbox and where textboxValidation_Validating is should be the name of your validating event. – coolmine Mar 22 '13 at 18:10
  • @kaus see my answer for a way to automatically do it for all text box controls on the form (the code wouldn't format correctly in the comments) – jerry Mar 22 '13 at 18:15
0

you can create a separator method for validation and call it on button click , try this:

private void SubmitButton_Click(object sender, EventArgs e)
    {
        if (ValidateControls()==0)
        {
           //Form is validated
        }
    }

    int ValidateControls()
    {
        int flag = 0;
        errorProvider1.Clear();
        if (txtAge.Text.Trim() == String.Empty)
        {
            errorProvider1.SetError(txtAge, "Age is required");
            flag = 1;
        }
        ............................................
        ............................................
       // validate all controls
        ............................................
        ............................................

        if (txtSalary.Text.Trim() == String.Empty)
        {
            errorProvider1.SetError(txtSalary, "Salary is required");
            flag = 1;
        }

        return flag;
    }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
0

If all your inputs are of the same type and you're okay with doing the same validation on each input and you name your controls consistently, coolmine's answer can be extended by adding something like the following code in your form's constructor (after initialization):

foreach(Control c in Controls)
{
    if(c is TextBox)
    {
        c.Tag = c.Name.Replace("txt","");
        c.Validating += textboxValidation_Validating;
    }
}
jerry
  • 2,581
  • 1
  • 21
  • 32