如何在 C# 的 "if" 语句中创建多个控件为假的方法?
How to create a method where multiple controls are false in an "if" statement for C#?
简化这个的最佳方法是什么(通过方法创建或其他方式):
if ((radioButton1.Checked == false) && (radioButton2.Checked == false) && (radioButton1.Checked == false) && ...more similar controls... && ((radioButton99.Checked == false))
{
MessageBox.Show("Please select an option!);
}
感谢您的考虑。对于造成的任何不便或不满,我们深表歉意。
private bool AreAllFalse(params bool[] thingsToCheck)
{
return things.All(t => !t);
}
用法
if (AreAllFalse(radioButton1.Checked,radiobutton2.Checked, etc)
{
//Do what you want
}
使用 params 关键字的好处是它会在您调用方法时为您创建数组。它还允许您只传递一件事来检查是否需要。虽然这在这里毫无意义,但它只是让事情变得灵活并且值得了解
可能的方法是为条件验证引入变量
bool optionIsSelected = ((radioButton1.Checked == true) || (radioButton2.Checked == true)...;
然后在if:
中使用
if (!optionIsSelected)
{
MessageBox.Show("Please select an option!);
}
您可以将所有这些控件放在一个列表中,然后检查是否选中了列表中的任何控件。这可以通过多种方式完成。下面是其中两个的示例。
使用循环的示例:
bool optionSelected = false;
foreach(var control in controls) // the List is in this case called controls
{
if(control.Checked)
{
optionSelected = true;
}
}
// Check the boolean
使用 System.Linq 的示例:
if(!controls.Any(c => c.Checked))
{
MessageBox.Show("Please select an option!);
}
您需要将控件添加到 public 集合中以简单地迭代它们。
如果你有一堆相同类型的控件,最好将它们放入表单构造函数的数组中:
CheckBox[] MyBoxes = new CheckBox[]{ check01, check02 , ... }
// MyBoxes is filled at Form_Load and it's usable in throughout of the form
bool result = true;
for(int i=0; i<MyBoxes.Length; i++)
{
if (MyBoxes[i].Checked == false)
{ result = false; break; }
}
另一个解决方案是遍历表单上的整个控件:
bool result = true;
for(int i=0; i<this.Controls.Count; i++)
{
if (this.Controls[i] is CheckBox)
{
if ((this.Controls[i] as CheckBox).Checked == false)
{ result = false; break; }
}
}
如果您有 很多 个控件(在本例中为 RadioButtons),那么您可以使用以下技巧 - Tag
属性.
总结一下:
1) 设置Tag
属性 和some string 以区别于其他控件(特别是,可能会出现并非必须处理一种类型的所有控件的情况)。
2) 在Tag
中收集这些带有定义字符串的控件并处理它们。
在您的特定情况下,您可以在 Tag
中设置字符串 ToCheck
,然后检查是否选中了所有 RadioButtons:
// Collect controls with defined string in Tag property
var radioButtons = this.Controls
.OfType<RadioButton>() //Filter controls by type
.Where(rb => ((string)rb.Tag) == "ToCheck"); //Get controls with defined string in Tag
// Check whether all RadioButtons are checked
bool allChecked = radioButtons.All(rb => rb.Checked);
简化这个的最佳方法是什么(通过方法创建或其他方式):
if ((radioButton1.Checked == false) && (radioButton2.Checked == false) && (radioButton1.Checked == false) && ...more similar controls... && ((radioButton99.Checked == false))
{
MessageBox.Show("Please select an option!);
}
感谢您的考虑。对于造成的任何不便或不满,我们深表歉意。
private bool AreAllFalse(params bool[] thingsToCheck)
{
return things.All(t => !t);
}
用法
if (AreAllFalse(radioButton1.Checked,radiobutton2.Checked, etc)
{
//Do what you want
}
使用 params 关键字的好处是它会在您调用方法时为您创建数组。它还允许您只传递一件事来检查是否需要。虽然这在这里毫无意义,但它只是让事情变得灵活并且值得了解
可能的方法是为条件验证引入变量
bool optionIsSelected = ((radioButton1.Checked == true) || (radioButton2.Checked == true)...;
然后在if:
if (!optionIsSelected)
{
MessageBox.Show("Please select an option!);
}
您可以将所有这些控件放在一个列表中,然后检查是否选中了列表中的任何控件。这可以通过多种方式完成。下面是其中两个的示例。
使用循环的示例:
bool optionSelected = false;
foreach(var control in controls) // the List is in this case called controls
{
if(control.Checked)
{
optionSelected = true;
}
}
// Check the boolean
使用 System.Linq 的示例:
if(!controls.Any(c => c.Checked))
{
MessageBox.Show("Please select an option!);
}
您需要将控件添加到 public 集合中以简单地迭代它们。 如果你有一堆相同类型的控件,最好将它们放入表单构造函数的数组中:
CheckBox[] MyBoxes = new CheckBox[]{ check01, check02 , ... }
// MyBoxes is filled at Form_Load and it's usable in throughout of the form
bool result = true;
for(int i=0; i<MyBoxes.Length; i++)
{
if (MyBoxes[i].Checked == false)
{ result = false; break; }
}
另一个解决方案是遍历表单上的整个控件:
bool result = true;
for(int i=0; i<this.Controls.Count; i++)
{
if (this.Controls[i] is CheckBox)
{
if ((this.Controls[i] as CheckBox).Checked == false)
{ result = false; break; }
}
}
如果您有 很多 个控件(在本例中为 RadioButtons),那么您可以使用以下技巧 - Tag
属性.
总结一下:
1) 设置Tag
属性 和some string 以区别于其他控件(特别是,可能会出现并非必须处理一种类型的所有控件的情况)。
2) 在Tag
中收集这些带有定义字符串的控件并处理它们。
在您的特定情况下,您可以在 Tag
中设置字符串 ToCheck
,然后检查是否选中了所有 RadioButtons:
// Collect controls with defined string in Tag property
var radioButtons = this.Controls
.OfType<RadioButton>() //Filter controls by type
.Where(rb => ((string)rb.Tag) == "ToCheck"); //Get controls with defined string in Tag
// Check whether all RadioButtons are checked
bool allChecked = radioButtons.All(rb => rb.Checked);