Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check multiple textboxes if null or empty without a unique test for each?

I have about 20 text fields on a form that a user can fill out. I want to prompt the user to consider saving if they have anything typed into any of the text boxes. Right now the test for that is really long and messy:

if(string.IsNullOrEmpty(txtbxAfterPic.Text) || string.IsNullOrEmpty(txtbxBeforePic.Text) ||
            string.IsNullOrEmpty(splitContainer1.Panel2) ||...//many more tests

Is there a way I could use something like an Array of any, where the array is made of the text boxes and I check it that way? What other ways might be a very convenient way in which to see if any changes have been made since the program started?

One other thing I should mention is there is a date time picker. I don't know if I need to test around that as the datetimepicker will never be null or empty.

EDIT: I incorporated the answers into my program, but I can't seem to make it work correctly. I set up the tests as below and keep triggering the Application.Exit() call.

        //it starts out saying everything is empty
        bool allfieldsempty = true;

        foreach(Control c in this.Controls)
        {
            //checks if its a textbox, and if it is, is it null or empty
            if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))
            {
                //this means soemthing was in a box
               allfieldsempty = false;
               break;
            }
        }

        if (allfieldsempty == false)
        {
            MessageBox.Show("Consider saving.");
        }
        else //this means nothings new in the form so we can close it
        {                
            Application.Exit();
        }

Why is it not finding any text in my text boxes based on the code above?

like image 272
Fuzz Evans Avatar asked Jan 05 '12 21:01

Fuzz Evans


People also ask

How check multiple TextBox is empty in C#?

foreach (Control c in this. Controls) { if (c is TextBox) { TextBox textBox = c as TextBox; if (textBox. Text == string. Empty) { // Text box is empty. // You COULD store information about this textbox is it's tag. } } }

How check multiple TextBox is empty or not in jQuery?

To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.

How do you check if all TextBox is empty in VB net?

First declare emptyTextBoxes as the selection of any text property with length 0. then check if there is any textbox with length = 0 and show it.


2 Answers

Sure -- enumerate through your controls looking for text boxes:

foreach (Control c in this.Controls)
{
    if (c is TextBox)
    {
        TextBox textBox = c as TextBox;
        if (textBox.Text == string.Empty)
        {
            // Text box is empty.
            // You COULD store information about this textbox is it's tag.
        }
    }
}
like image 68
George Johnston Avatar answered Sep 30 '22 22:09

George Johnston


Building on George's answer, but making use of some handy LINQ methods:

if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))  
{
//Your textbox is empty
}
like image 45
StriplingWarrior Avatar answered Sep 30 '22 21:09

StriplingWarrior