Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How should I declare "validate only Text Boxes which are enabled?"

C#
 public static bool IsEmpty(Form myform)
        {
            foreach (Control myControl in myform.Controls)
            {
if ((myControl.GetType() == typeof(TextBox)) || (myControl.GetType() == typeof(ComboBox)))
                {
                    myControl.Text = myControl.Text.Trim();
                    if (myControl.Text == "")
                    {
                        MessageBox.Show(myControl.Name.Substring(3).ToString() + " is empty.");
                        myControl.Focus();
                        return true;
                    }
                    myControl.Text = TrimBetween(myControl.Text);
                }
Posted
Updated 7-Jul-12 19:17pm
v3

1 solution

Add a check for the Enabled property:
C#
if ((myControl.GetType() == typeof(TextBox)) || (myControl.GetType() == typeof(ComboBox)))
    {
    //myControl.Text = myControl.Text.Trim();
    if (myControl.Enabled && string.IsNullOrWhiteSpace(myControl.Text))
        {
        MessageBox.Show(myControl.Name.Substring(3).ToString() + " is ");
        myControl.Focus();
        return true;
        }
    myControl.Text = TrimBetween(myControl.Text);
    }
BTW: I have removed the Trim, and changed the check - if you use the IsNullOrWhiteSpace method it does that for you.
 
Share this answer
 

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