Donavon Decker
Donavon Decker

Reputation: 39

Selecting more than 1 text box to check if it is empty

So I made a small application. There is 6 sections of text boxes, 3 in each section.

string location;

Random lc = new Random();

byte i5 = (byte)lc.Next(3);
switch (i5)
{
    case 0:
        location = l1.Text;
        break;
    case 1:
        location = l2.Text;
        break;
    case 2:
        location = l3.Text;
        break;
}

I am wondering how to make a check to see if each box has a word in it - if even 'one' box is empty, then I don't want it to execute. If all three are filled in, then it can continue executing.

Upvotes: 0

Views: 342

Answers (2)

TGH
TGH

Reputation: 39248

I would do this

if(this.Controls.OfType<TextBox>().All(t => string.IsNullOrEmpty(t.Text) == false))
{
  //carry out logic
}

"this.Controls" refers to the parent control of the textboxes

Upvotes: 3

JohnFx
JohnFx

Reputation: 34909

Seems simple enough...

if String.IsNullOrEmpty(l1.Text) return;
if String.IsNullOrEmpty(l2.Text) return;
if String.IsNullOrEmpty(l3.Text) return;
if ....

Upvotes: 2

Related Questions