Click here to Skip to main content
11,701,616 members (65,481 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
How Can i validate many text boxes at a time by set errorprovider on those textboxes.
i want to use it on butten click event.
Posted 31-Jan-13 22:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi... You can do it by this way...

public bool CheckTextBoxNull(TextBox txt)
       {
           if (txt.Text.Trim() == "" || txt.Text==string.Empty())
           {
               txt.BackColor = Color.FromArgb(255, 192, 192);
               txt.Focus();
               return false;
           }
           else
           {
               return true;
           }
       }
 

private bool ValidateFn()
        {
           #region Object Validate Function

           if (CheckTextBoxNull(textbox1) == false) { return false; }
 
           if (CheckTextBoxNull(textbox2) == false) { return false; }
           return true;
        }
 
private void btn_Save_Click(object sender, EventArgs e)
        {
            #region Save Funtion
            if (ValidateFn() == true)
            {
                // you Code here...
            #endregion
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

StringBuilder errorMessage = new StringBuilder();
foreach(Control c in Page.Controls) 
{ 
    if (c is TextBox) 
    { 
        if(ur conditions)
        {
          errorMessage.Append(c.ID.ToString()+"Has Error");
        }        
    } 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can create method given below for all similar kind of controls. You can call this method by passing array of control i.e in your case you can call like given below (pass all textboxs which has to validated)

TextBox[] listTextBox = {textBox1,textBox2};

public bool CheckTextBoxNull(TextBox[] listTextBox)
   {
       for (int i = 0; i <= listTextBox.Length - 1; i++)
       {
           if (listTextBox[i].Text.Trim().Length == 0)
           {
               listTextBox[i].BackColor = System.Drawing.Color.FromArgb(255, 192, 192);
               listTextBox[i].Focus();
               return false;
           }
       }
       return true;
   }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 320
1 OriginalGriff 294
2 Maciej Los 275
3 Mika Wendelius 159
4 So_Keeee 100
0 OriginalGriff 8,324
1 Sergey Alexandrovich Kryukov 7,422
2 CPallini 4,912
3 Maciej Los 4,196
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100