LindseyM
LindseyM

Reputation: 21

Multiple Textbox Validations

So I have a form with many TextBox-es which all need to be filled in. I have researched textbox validation but I can only find instructions for validating singular text boxes. Below is the code I have for the singular textbox validation. I was just wondering if it possible to hit all of them at once instead of this for each one. Any help would be much appreciated!

private void txtName_Validating(object sender, CancelEventArgs e)
{
    if (string.IsNullOrEmpty(txtName.Text.Trim()))
    {
        epName.SetError(txtName, "Name is required.");
    }
    else
    {
        epName.SetError(txtName, string.Empty);
    }
}

Upvotes: 1

Views: 184

Answers (2)

Noob Coder
Noob Coder

Reputation: 524

Assuming you are using WinForms

// Get all the controls of the forms
var controls = this.Controls;
foreach (Control mycontrol in controls)
{
    // Check if the Control is a TextBox
    if (mycontrol is TextBox)
    {
     //Perform Operation
    }
}

Upvotes: 3

abhay bhadoriya
abhay bhadoriya

Reputation: 41

var controls = this.Controls;
        foreach (Control mycontrol in controls)
        {
            // Check if the Control is a TextBox
            if (mycontrol is TextBox)
            {
                epname.seterror(mycontrol, mycontrol+"is required");
            }
        }

Upvotes: 0

Related Questions