How can I check if all textbox is empty? [closed]

0

Code:

if (string.IsNullOrWhiteSpace(txtNumero.Text) && string.IsNullOrWhiteSpace(txtTitle.Text))
{
    MessageBox.Show("Preencha todas as informações");
}

I tried to do with & and it does not work, how can I do it? I want to check two or more textboxes

    
asked by anonymous 26.09.2017 / 15:01

1 answer

2

If you want to check all textBoxes the best way and create a dynamic method (imagine a signup screen where you have a lot of textBoxes )

private bool textBoxVazias()
{
   foreach (Control c in this.Controls)
      if (c is TextBox)
      {
         TextBox textBox = c as TextBox;
         if (string.IsNullOrWhiteSpace(textBox.Text))
            return true;
      }
    return false;
}

After creating the method, just call it and if the return is positive, some textBox is empty.

if(textBoxVazias()) MessageBox.Show("Preencha todas as informações");

If you still want to specify the fields, or if they are as few as in your example, just use || (or) instead of & (e)

if (string.IsNullOrWhiteSpace(txtNumero.Text) || string.IsNullOrWhiteSpace(txtTitle.Text))
    MessageBox.Show("Preencha todas as informações");
    
26.09.2017 / 15:29