Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,
i have a question about checking text boxes for null,
i want to check many text box in all text boxes on the form,
how can i do it?
i use this code but it check all text boxes on the form.
C#
private void button1_Click(object sender, EventArgs e)
        {
            bool isIncomplete = false;
            foreach (Control ctls in this.Controls)
            {
                if (ctls is TextBox)
                {
                    TextBox tb = ctls as TextBox;
                    if(string.IsNullOrWhiteSpace(tb.Text))
                    {
                        isIncomplete = true;
                        break;
                    }

                }


            }
            if (isIncomplete)
            {
                MessageBox.Show("please fill all text boxes!");
            }
        }
Posted
Comments
[no name] 5-Sep-13 11:16am    
"i use this code but it check all text boxes on the form" which is what you say you want so what is the problem?

If you only want to check some of the text boxes, instead of all of them then you need to have some way to specify "check this one" or "Don't check this one". One of teh simplest ways to do that is to use the Control.Tag property.

By default, the Tag is null, so you could give a value to those you do want to check, (or just to those you don't - whichever you prefer). You can set the Tag at design or run time, and it can contain any object you want it to.
C#
foreach (Control c in Controls)
    {
    if (c.Tag != null && c is TextBox)
        {
        TextBox t = c as TextBox;
        if (string.IsNullOrWhiteSpace(t.Text))
            {
            isIncomplete = true;
            break;
            }
        }
    }
 
Share this answer
 
It is impossible to have a null check box, if it is added to some control of your UI. As it can be seen from your code, you are trying to check up the property System.Window.Forms.TextBox.Text, but this text is also never null, so all you need is to check it by comparison with string.Empty (which is of course not null, just empty string!).

Your code lacks recursion. The problem is: some of the text boxes can be placed not directly on form, but on some other controls (panels). Actually, it's recommended to put everything on some hierarchy of panels and use docking. As a matter of thumb rule, never use manual placement.

To take it into account, your code should be something like that:

C#
static TextBox FindEmptyTextBox(Control parent) {
    TextBox textBox = parent as TextBox;
    if (textBox != null) { // successful dynamic cast, parent is a TextBox
        it (textBox.Text == string.Empty) // variant: if (textBox.Text.Trim() == string.Empty)
            return textBox;
    } else
        foreach(Control child in parent.Controls) {
            TextBox childTextBox = FindEmptyTextBox(child);
            if (childTextBox != null)
                return childTextBox;
        } //loop
    return null;
} // FindEmptyTextBox


This code will find the first empty text box encountered, hierarchically. It it returns null, there are no text boxed parented by a control, or they are all non-empty. If you apply this method to a form, it will find the first encountered text box on this form, hierarchically. Having the reference to a found control can be important, as you can focus on it and tell the user to input text in it (instead of ugly suggestion to the user to find all of them).

—SA
 
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