简体   繁体   中英

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.

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

Seems simple enough...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM