C# 复选框的 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11849930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If statements for Checkboxes
提问by user1512593
I wanted to know how to write if statements to see if one or another check box is checked or not.
我想知道如何编写 if 语句以查看是否选中了一个或另一个复选框。
I have two check boxes. I wanted it to check to see if checkbox 1 is checked and checkbox 2 is null then call this function, and if checkbox 2 is checked and checkbox 1 is null then call another function.
我有两个复选框。我想让它检查复选框 1 是否被选中并且复选框 2 为空然后调用这个函数,如果复选框 2 被选中并且复选框 1 为空然后调用另一个函数。
Pretty bad with IF statements and not sure how to convert the checkbox into a readable value.
IF 语句非常糟糕,并且不确定如何将复选框转换为可读值。
采纳答案by Science_Fiction
I'm making an assumption that you mean not checked. I don't have a C# compiler handy but:
我假设你的意思是没有检查。我手边没有 C# 编译器,但是:
if (checkbox1.Checked && !checkbox2.Checked)
{
}
else if (!checkbox1.Checked && checkbox2.Checked)
{
}
回答by DROP TABLE users
Your going to use the checkbox1.checkedproperty in your if statement, this returns true or false depending on weather it is checked or not.
您将checkbox1.checked在 if 语句中使用该属性,根据检查与否的天气返回 true 或 false。
回答by Sanjay
In VB.Net
在 VB.Net 中
If Check1.checked and Not (Check2.checked) Then
ElseIf Check2.Checked and not Check1.Checked then
End If
回答by Mike
I simplification for Science_Fiction's answer I think is to use the exclusive or function so you can just have:
我简化了 Science_Fiction 的答案,我认为是使用exclusive or 函数,这样您就可以拥有:
if(checkbox1.checked ^ checkbox2.checked)
{
//do stuff
}
That is assuming you want to do the same thing for both situations.
那是假设您想对两种情况都做同样的事情。
回答by Shailendra Mishra
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxImage.Checked)
{
groupBoxImage.Show();
}
else if (!checkBoxImage.Checked)
{
groupBoxImage.Hide();
}
}

