我有几个复选框和一个文本框。当我选中某个复选框时,它将禁用文本框和其他指定的复选框。
该代码适用于文本框。当我添加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;
}
答案 0 :(得分:6)
你忘了在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;
}
答案 1 :(得分:1)
您需要围绕其他人使用括号,如下所示:
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;
答案 2 :(得分:1)
在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;
}
}