多条件 if 语句

发布于 2024-11-08 19:34:05 字数 726 浏览 0 评论 0原文

我可以发誓我知道如何做到这一点......无论如何,我想检查一个控件是否与其他两个控件的名称匹配。如果它与任一控件名称匹配,我希望它中止“任务”(如果您愿意的话)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2024-11-15 19:34:05

你有||与负面条件相结合。这就像在说:“如果我的名字不是乔恩,那就不是杰夫。”嗯,不可能两者兼而有之,所以这个条件永远为真。我怀疑你真的想要:

// Do you really need to check names instead of just references?
// You could probably just use
// if (myControl != checkBox3 && myControl != panel7)
if (myControl.Name != checkBox3.Name && myControl.Name != panel7.Name)
{
    // No need for your if block here
    myControl.Enabled = checkBox3.Checked;
}

我也鼓励你总是使用大括号,即使对于单语句 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:

// Do you really need to check names instead of just references?
// You could probably just use
// if (myControl != checkBox3 && myControl != panel7)
if (myControl.Name != checkBox3.Name && myControl.Name != panel7.Name)
{
    // No need for your if block here
    myControl.Enabled = checkBox3.Checked;
}

I would also encourage you to always use braces, even for single-statement if bodies - that makes it clearer that the foreach isn't meant to be part of the if body.

奢望 2024-11-15 19:34:05

您的 if 语句将始终返回 true (假设 checkBox3 和 panel7 有不同的名称)。

我认为你想要的是以下之一:

if (myControl.Name != checkBox3.Name && myControl.Name != panel7.Name)

或:

if (!(myControl.Name == checkBox3.Name || myControl.Name == panel7.Name))

Your if statement will always return true (assuming checkBox3 and panel7 have different names).

I think what you want is one of:

if (myControl.Name != checkBox3.Name && myControl.Name != panel7.Name)

or:

if (!(myControl.Name == checkBox3.Name || myControl.Name == panel7.Name))
把时间冻结 2024-11-15 19:34:05

阅读英文版会有帮助
你拥有的是:
 如果 myControl.Name 不等于 checkbox3.name 或不等于 panel7.name
你想要的是:
如果 myControl.Name 不等于 checkbox3.name 或等于 panel7.name

if (!(myControl.Name == checkBox3.Name || myControl.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

if (!(myControl.Name == checkBox3.Name || myControl.Name == panel7.Name))
書生途 2024-11-15 19:34:05

这是我最终使用的:

private void DisableMessageControls(Control myControl)
{
    if (myControl.Name == checkBox3.Name || myControl.Name == panel7.Name || myControl.Name == tpMessage.Name)
    {

    }
    else
    {
        myControl.Enabled = checkBox3.Checked;
    }

    foreach (Control myChild in myControl.Controls)
        DisableMessageControls(myChild);

}

Here is what I ended up using:

private void DisableMessageControls(Control myControl)
{
    if (myControl.Name == checkBox3.Name || myControl.Name == panel7.Name || myControl.Name == tpMessage.Name)
    {

    }
    else
    {
        myControl.Enabled = checkBox3.Checked;
    }

    foreach (Control myChild in myControl.Controls)
        DisableMessageControls(myChild);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文