简体   繁体   English

在单个If语句中检查多个条件-C#

[英]Checking multiple conditions in a single If statement - C#

I have a form in which there are many textBoxes from which three textboxes are required to fill in order to submit the form. 我有一个表单,其中有许多文本框,需要从其中填写三个文本框才能提交表单。 I dont want to use each If block for each text box. 我不想为每个文本框使用每个If块。 Is there any way to use a single if statement for all the three text boxes? 是否可以对所有三个文本框使用单个if语句? I am using the following code: 我正在使用以下代码:

if (textBox1.Text != "" || textBox2.Text != "" || textBox4.Text != "")
{
   // Code
}
else
{
   MessageBox.Show("Fill required fields");
}

but this code works even a single text fox is filled and the rest of the required text boxes are empty. 但是即使填充了单个文本狐狸,此代码也可以正常工作,而其余所需的文本框为空。

 if (textBox1.Text != "" &&  textBox2.Text != "" && textBox4.Text != "")
 {
     // Code
 }
 else
 {
     MessageBox.Show("Fill required fields");
 }

You want all conditions to pass. 您希望所有条件都通过。 This fits the semantics of the logical AND operator && . 这符合逻辑AND运算符&&的语义。

If you have tons of textboxes, I would tend to keep them in a list: 如果您有大量的文本框,我倾向于将它们保留在列表中:

var boxes = new List<TextBox>{
     textBox1,
     textBox2,
     textBox3,
     //...
};
if (boxes.Any(tb => string.IsNullOrEmpty(tb.Text)))
{
     MessageBox.Show("Fill required fields");
}
else
{
    // Code
}

I also tend to prefer to keep the exception in the if part, returning or throwing an error and ommit an else part, since that is just normal code flow. 我也倾向于将异常保留在if部分中,返回或抛出错误并省略else部分,因为那只是正常的代码流。 This keeps the code you expect to run as far to the left as possible. 这样可以使您希望运行的代码尽可能左移。

You should change || 您应该更改|| to && &&

You created a collection of or statements, so only one needs to be true in order to continue. 您创建了or语句的集合,因此只有一个为真才能继续。 Instead you need to and them: 相反,您需要and他们:

if (textBox1.Text != "" && textBox2.Text != "" && textBox4.Text != "")

You may define a method to test empty strings. 您可以定义一种方法来测试空字符串。

public class Test
{   
  public static bool IsEmpty(params string []args)
    {
        if (args.Length == 0) return true ;
        return args.Any(p => string.IsNullOrEmpty(p));
    }
}

To test strings, 要测试字符串,

if(!Test.IsEmpty(TextBox1.Text,TextBox2.Text,TextBox3.Text))
 {
   //valid
 }

You use OR (||) and should use AND (&&) instead. 您使用OR(||),而应使用AND(&&)。 You want ALL three textboxes to be non-empty strings. 您希望所有三个文本框都是非空字符串。 Check out the following code: 查看以下代码:

if (textBox1.Text != String.Empty && textBox2.Text != String.Empty && textBox4.Text != String.Empty)
{
  // Code
}
else
{
  MessageBox.Show("Fill required fields");
}

You can also make a collection of TextBoxes and loop through them to check for non-empty Strings. 您还可以收集TextBoxes并遍历它们,以检查非空字符串。 Something like this: 像这样:

List<TextBox> _lstTextBoxes = new List<TextBox>();
_lstTextBoxes.Add(textBox1);
_lstTextBoxes.Add(textBox2);
_lstTextBoxes.Add(textBox3);

Boolean checkFailed = false;
foreach(TextBox tb in _lstTextBoxes)
  if(tb.Text == String.Empty)
    checkFailed = true;

if(checkFailed)
  MessageBox.Show("Fill required fields");
else
  //code

This way you have a more generic approach to which you can easily add or remove certain textboxes. 这样,您可以采用一种更通用的方法,可以轻松地添加或删除某些文本框。

Instead of using OR (||) use AND (&) in your condition. 代替使用OR (||) AND (&)在您的情况下使用AND (&)

Suggestion 建议

  • Use Trim function from string to remove any white spaces from textbox (if required) 使用string Trim函数从文本框中删除所有空格(如果需要)
  • Instead of comparing like textBox1.Text != "" do String.IsNullOrEmpty(textBox1.Text) == false 而不是像textBox1.Text != ""那样比较String.IsNullOrEmpty(textBox1.Text) == false

Assuming you have 4 textboxes in total, below code will work 假设您总共有4个文本框,下面的代码将起作用

if ((textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "") || (textBox1.Text != "" && textBox2.Text != "" && textBox4.Text != "") ||
        (textBox1.Text != "" && textBox3.Text != "" && textBox4.Text != "") || (textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
        )
    {                  
        // Code             
    }             
    else             
    {                 
        MessageBox.Show("Fill required fields");             
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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