在CheckedChange事件上禁用/启用若干项

本文关键字:启用 CheckedChange 事件 | 更新日期: 2023-09-27 18:08:37

我有几个复选框和一个文本框。当我选中某个复选框时,它将禁用文本框和其他指定的复选框。

该代码在文本框中运行良好。当我添加else语句时,它仍然适用于文本框,但不适用于复选框。如果我省略else,只使用if来禁用它们,它就会起作用。如果复选框未选中,我需要它重新启用它们。

我在一个面板中有所有问题的复选框。

    private void noModemChkbox_CheckedChanged(object sender, EventArgs e)
    {
        if (noModemChkbox.Checked == true)
        {
            custModemText.Enabled = false;
            pwrCbx.Enabled = false;
            e1Cbx.Enabled = false;
            e2Cbx.Enabled = false;
            e3Cbx.Enabled = false;
            e4Cbx.Enabled = false;
            dslblinkCbx.Enabled = false;
            enetCbx.Enabled = false;
            dslCbx.Enabled = false;
            inetCbx.Enabled = false;
            inetredCbx.Enabled = false;
            wlanCbx.Enabled = false;
            activityChkbox.Enabled = false;
        }
        else
            custModemText.Enabled = true;
        pwrCbx.Enabled = true;
        e1Cbx.Enabled = true;
        e2Cbx.Enabled = true;
        e3Cbx.Enabled = true;
        e4Cbx.Enabled = true;
        dslblinkCbx.Enabled = true;
        enetCbx.Enabled = true;
        dslCbx.Enabled = true;
        inetCbx.Enabled = true;
        inetredCbx.Enabled = true;
        wlanCbx.Enabled = true;
        activityChkbox.Enabled = true;
    }

在CheckedChange事件上禁用/启用若干项

你忘记用大括号括住else语句了:

private void noModemChkbox_CheckedChanged(object sender, EventArgs e)
{
    if (noModemChkbox.Checked == true)
    {
        custModemText.Enabled = false;
        pwrCbx.Enabled = false;
        e1Cbx.Enabled = false;
        e2Cbx.Enabled = false;
        e3Cbx.Enabled = false;
        e4Cbx.Enabled = false;
        dslblinkCbx.Enabled = false;
        enetCbx.Enabled = false;
        dslCbx.Enabled = false;
        inetCbx.Enabled = false;
        inetredCbx.Enabled = false;
        wlanCbx.Enabled = false;
        activityChkbox.Enabled = false;
    }
    else
    {
        custModemText.Enabled = true;
        pwrCbx.Enabled = true;
        e1Cbx.Enabled = true;
        e2Cbx.Enabled = true;
        e3Cbx.Enabled = true;
        e4Cbx.Enabled = true;
        dslblinkCbx.Enabled = true;
        enetCbx.Enabled = true;
        dslCbx.Enabled = true;
        inetCbx.Enabled = true;
        inetredCbx.Enabled = true;
        wlanCbx.Enabled = true;
        activityChkbox.Enabled = true;
    }
}

这样不是更简单吗?:

private void noModemChkbox_CheckedChanged(object sender, EventArgs e)
{
    bool bEnabled = !noModemChkbox.Checked;
    custModemText.Enabled = bEnabled;
    pwrCbx.Enabled = bEnabled;
    e1Cbx.Enabled = bEnabled;
    e2Cbx.Enabled = bEnabled;
    e3Cbx.Enabled = bEnabled;
    e4Cbx.Enabled = bEnabled;
    dslblinkCbx.Enabled = bEnabled;
    enetCbx.Enabled = bEnabled;
    dslCbx.Enabled = bEnabled;
    inetCbx.Enabled = bEnabled;
    inetredCbx.Enabled = bEnabled;
    wlanCbx.Enabled = bEnabled;
    activityChkbox.Enabled = bEnabled;
}

else需要用括号括起来,如下所示:

else
{
    custModemText.Enabled = true;
    pwrCbx.Enabled = true;
    e1Cbx.Enabled = true;
    e2Cbx.Enabled = true;
    e3Cbx.Enabled = true;
    e4Cbx.Enabled = true;
    dslblinkCbx.Enabled = true;
    enetCbx.Enabled = true;
    dslCbx.Enabled = true;
    inetCbx.Enabled = true;
    inetredCbx.Enabled = true;
    wlanCbx.Enabled = true;
    activityChkbox.Enabled = true;
}

你的原始代码实际上是这样解释的:

else
{
    custModemText.Enabled = true;
}
pwrCbx.Enabled = true;
e1Cbx.Enabled = true;
e2Cbx.Enabled = true;
e3Cbx.Enabled = true;
e4Cbx.Enabled = true;
dslblinkCbx.Enabled = true;
enetCbx.Enabled = true;
dslCbx.Enabled = true;
inetCbx.Enabled = true;
inetredCbx.Enabled = true;
wlanCbx.Enabled = true;
activityChkbox.Enabled = true;

在else子句中需要{}括号(如果有多个语句):

private void noModemChkbox_CheckedChanged(object sender, EventArgs e)
{
    if (noModemChkbox.Checked == true)
    {
        custModemText.Enabled = false;
        pwrCbx.Enabled = false;
        e1Cbx.Enabled = false;
        e2Cbx.Enabled = false;
        e3Cbx.Enabled = false;
        e4Cbx.Enabled = false;
        dslblinkCbx.Enabled = false;
        enetCbx.Enabled = false;
        dslCbx.Enabled = false;
        inetCbx.Enabled = false;
        inetredCbx.Enabled = false;
        wlanCbx.Enabled = false;
        activityChkbox.Enabled = false;
    }
    else
    {
        custModemText.Enabled = true;
        pwrCbx.Enabled = true;
        e1Cbx.Enabled = true;
        e2Cbx.Enabled = true;
        e3Cbx.Enabled = true;
        e4Cbx.Enabled = true;
        dslblinkCbx.Enabled = true;
        enetCbx.Enabled = true;
        dslCbx.Enabled = true;
        inetCbx.Enabled = true;
        inetredCbx.Enabled = true;
        wlanCbx.Enabled = true;
        activityChkbox.Enabled = true;
    }
}