如果选中一个复选框,则将另一复选框设置为未选中 [英] If one checkbox is checked, set the other to unchecked

查看:84
本文介绍了如果选中一个复选框,则将另一复选框设置为未选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有两个复选框; chkBuried chkAboveGround 我要设置它,因此如果一个选中,则另一个未被选中。我该怎么做?

I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?

我已经尝试过 CheckChanged 属性:

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    chkAboveGround.Checked = false;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    chkBuried.Checked = false;
}

它有效,只是不如我希望的那样。也就是说,当我选中 chkBuried ,然后选中 chkAboveGround 时,两个框都变为未选中状态,然后才能再次检查另一个。

And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.

推荐答案

按如下所示修改您的代码。

modify your code as below.

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    chkBuried.Checked = !chkAboveGround.Checked;
}

这篇关于如果选中一个复选框,则将另一复选框设置为未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆