多条件 if 语句
我可以发誓我知道如何做到这一点......无论如何,我想检查一个控件是否与其他两个控件的名称匹配。如果它与任一控件名称匹配,我希望它中止“任务”(如果您愿意的话)。
private void DisableMessageControls(Control myControl)
{
// if the control is checkbox3 OR panel7 I do NOT want to execute the below code!
if (myControl.Name != checkBox3.Name || myControl.Name != panel7.Name)
if (checkBox3.Checked == true)
{
myControl.Enabled = true;
}
else
{
myControl.Enabled = false;
}
foreach (Control myChild in myControl.Controls)
DisableMessageControls(myChild);
}
I could have sworn I knew how to do this ... regardless, I want to check if a control matches the name of two other controls. If it matches either of the control's names, I want it to abort the "mission" if you will.
private void DisableMessageControls(Control myControl)
{
// if the control is checkbox3 OR panel7 I do NOT want to execute the below code!
if (myControl.Name != checkBox3.Name || myControl.Name != panel7.Name)
if (checkBox3.Checked == true)
{
myControl.Enabled = true;
}
else
{
myControl.Enabled = false;
}
foreach (Control myChild in myControl.Controls)
DisableMessageControls(myChild);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有||与负面条件相结合。这就像在说:“如果我的名字不是乔恩或,那就不是杰夫。”嗯,不可能两者兼而有之,所以这个条件永远为真。我怀疑你真的想要:
我也鼓励你总是使用大括号,即使对于单语句
if
主体 - 这使得foreach
更清楚code> 并不意味着成为if
主体的一部分。You've got || combined with negative conditions. It's like saying, "If my name isn't Jon or it isn't Jeff." Well it can't be both, so that condition will always be true. I suspect you really want:
I would also encourage you to always use braces, even for single-statement
if
bodies - that makes it clearer that theforeach
isn't meant to be part of theif
body.您的 if 语句将始终返回
true
(假设 checkBox3 和 panel7 有不同的名称)。我认为你想要的是以下之一:
或:
Your if statement will always return
true
(assuming checkBox3 and panel7 have different names).I think what you want is one of:
or:
阅读英文版会有帮助
你拥有的是:
如果 myControl.Name 不等于 checkbox3.name 或不等于 panel7.name
你想要的是:
如果 myControl.Name 不等于 checkbox3.name 或等于 panel7.name
reading it in english can help
what you have is:
if myControl.Name is not equal to checkbox3.name or is not equal to panel7.name
what you want is:
if myControl.Name is not equal to checkbox3.name or equal to panel7.name
这是我最终使用的:
Here is what I ended up using: