Ashraf Sada
Ashraf Sada

Reputation: 4905

Using Linq to loop through all controls only get the first control

I have a form with 5 text boxes and a button, I want to check all these text boxes for empty or null user input. Using this simple method:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (verifyUI() == true)
                MessageBox.Show("user input for all textboxes was correct!");
            else
                MessageBox.Show("user input for all textboxes was missing!");
        }

        private bool verifyUI()
        {
            bool userInputOk = false;
            foreach (Control cTxt in Controls.OfType<TextBox>())
            {
                if (string.IsNullOrWhiteSpace(cTxt.Text) || cTxt.Text == "")
                {
                    userInputOk = false;
                }
                else
                {
                    userInputOk = true;
                }
            }
            return userInputOk;
        }
    }

When I enter a value in text box 1 , the method is checking only the first text box and returning true, and ignoring all other text boxes.

I am sure something is wrong in the logic of the method which I am using.

Upvotes: 2

Views: 1171

Answers (6)

Sagar
Sagar

Reputation: 137

I think just for checking whether any textbox is not filled. following code is enough.

        private bool verifyUI()
        {
            bool alluserInputsOk = true;
            foreach (Control cTxt in Controls.OfType<TextBox>())
            {
                if (string.IsNullOrWhiteSpace(cTxt.Text))
                {
                    userInputOk = false;
                    break;
                }                   
            }
            return userInputOk;
        }

or You can use .any() method with your list

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

It seems you want to know if any input is wrong (or all the input is correct):

  private bool verifyUI() {
    return !Controls
      .OfType<TextBox>() 
      .Any(cTxt => string.IsNullOrWhiteSpace(cTxt.Text));
  }

or (equivalent All() implementation)

  private bool verifyUI() {
    return Controls
      .OfType<TextBox>() 
      .All(cTxt => !string.IsNullOrWhiteSpace(cTxt.Text));
  }

In your current code you constantly re-write userInputOk and so return the last value

Upvotes: 8

D Stanley
D Stanley

Reputation: 152626

Actually, it's probably looping though all controls, but only returning the result of the last one, since you set the value of userInputOk back to true if the control has valid text. So the end result is the result for the last control. Now it's possible that textBox1 is the last control in the collection, depending on how they were added. You can either just remove the else block, which will flag userInputOk only if a control has an invalid value, or use Linq:

 bool userInputOk = 
          !Controls.OfType<TextBox>()
                   .Any(cTxt => string.IsNullOrWhiteSpace(cTxt.Text))

Upvotes: 0

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43906

Your code checks only the last textbox it finds. Try this All() statement instead:

bool userInputOk = Controls.OfType<TextBox>()
                           .All(tb => !string.IsNullOrWhiteSpace(tb.Text));

Note that string.IsNullOrWhiteSpace() is true for string.Empty, too. So you don't need that extra check for string.Empty.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245479

The easiest way would be to use the All method:

private bool verifyUI()
{
    return Controls.OfType<TextBox>().All(tb => !string.IsNullOrWhiteSpace(tb.Text));
}

You could also invert the logic and use the Any method as well. I prefer All in this case simply because it conveys the intention better and makes the logic more clear for the next person.

Upvotes: 2

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

Your code is actually only checking whether the last control in the list is blank or not, since that's where your iteration ends.

Upvotes: 1

Related Questions