我想当我选中一个复选框时,另一个复选框在C#中变为未选中状态 [英] i want when i checked a checkbox aother checkbox becomes unchecked in c#

查看:79
本文介绍了我想当我选中一个复选框时,另一个复选框在C#中变为未选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的主题很完整:)


感谢您的时间

i think my subject is very complete explanation :)


thank u for time

推荐答案

您可以执行以下操作..

you can do something like this..

if(checkbox1.Checked)
checkbox2.checked=false;
else if(!checkbox1.checked)
checkbox2.checked=true;


,我假设您只选择了Windows应用程序的C#标签.
现在有两个选项.
1.您可以使用CheckedListBox控件(如果处于设计阶段),然后可以在其中选择属性,以便仅允许一个复选框选择
2.您需要在表单加载时编写代码
Since, you have selected only C# tag, I assume, its windows application.
There are two options now.
1. You can use CheckedListBox control (if you are in design phase) and there you can select property so it allows only one check box selection
2. You need to write code on form load
ChkBox1.CheckedChanged += new EventHandler(OnCheckedChanged);
ChkBox2.CheckedChanged += new EventHandler(OnCheckedChanged); 



现在所有复选框将指向相同的事件处理函数.

在这个偶数处理程序中,您编写



Now all check box will point to same event handler function.

In this even handler, you write

private void OnCheckedChanged(object sender, EventArgs e)
{
  CheckBox clickedCheckBox = (CheckBox) sender;
  if(clickedCheckBox.Checked)
  {
    if(clickedCheckBox.Name = "ChkBox1";
       ChkBox2.Checked = false;
    else
       ChkBox1.Checked = false;
  }
}


第二种解决方案的唯一问题是,如果您拥有2-3个以上的复选框,那将是一场噩梦.

希望能有所帮助.如果是这样,请将其标记为解决方案.
谢谢
Milind


The only problem with second solution, it is going to be nightmare if you have more than 2-3 checkboxes.

Hope that helps. If it does, mark it as solution.
Thanks
Milind


好,您必须编写一些代码:
ok you have to write some code:
if(chkbox1.checked==true)
{
 chkbox2.checked==false;
 chkbox3.checked==false;
}
else if(chkbox1.checked==false)
{
  chkbox2.checked==true;
 chkbox3.checked==true;
}



在chkbox1的已检查事件上编写此代码



Write this code on checked event of chkbox1


这篇关于我想当我选中一个复选框时,另一个复选框在C#中变为未选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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