C# 只选择一个复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19358419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only one checkbox to be selected
提问by grimsan55
I would like to only have single checkbox selected at a time. My program reads from a textfile and creates checkboxes according to how many "answers" there are in the textfile.
我只想一次选择一个复选框。我的程序从文本文件中读取并根据文本文件中有多少“答案”创建复选框。
Does anyone know what is wrong with the code?
有谁知道代码有什么问题?
public partial class Form1 : Form
{
string temp = "questions.txt";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(temp);
string line = "";
List<string> enLista = new List<string>();
while ((line = sr.ReadLine()) != null)
{
string[] myarray = line.Split('\r');
enLista.Add(myarray[0]);
}
sr.Close();
for (int i = 0; i < enLista.Count; i++)
{
if (enLista[i].Contains("?"))
{
Label lbl = new Label();
lbl.Text = enLista[i].ToString();
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
}
else if (enLista[i] == "")
{
}
else
{
CheckBox chk = new CheckBox();
chk.Text = enLista[i].ToString();
flowLayoutPanel1.Controls.Add(chk);
chk.Click += chk_Click;
}
}
}
private void chk_Click(object sender, EventArgs e)
{
CheckBox activeCheckBox = sender as CheckBox;
foreach (Control c in Controls)
{
CheckBox checkBox = c as CheckBox;
if (checkBox != null)
{
if (!checkBox.Equals(activeCheckBox))
{ checkBox.Checked = !activeCheckBox.Checked; }
else
{ checkBox.Checked = true; }
}
}
}
}
采纳答案by King King
It's so simple to achieve what you want, however it's also so strange:
实现你想要的很简单,但它也很奇怪:
//We need this to hold the last checked CheckBox
CheckBox lastChecked;
private void chk_Click(object sender, EventArgs e) {
CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox != lastChecked && lastChecked!=null) lastChecked.Checked = false;
lastChecked = activeCheckBox.Checked ? activeCheckBox : null;
}
回答by Ssc456
I think you are looking for Radio Buttons.
我认为您正在寻找单选按钮。
If you are insistent on using checkboxes then change your event to CheckedChanged as this will be more accurate. Check boxes can unfortunately be clicked without checking themselves!
如果您坚持使用复选框,那么将您的事件更改为 CheckedChanged,因为这会更准确。不幸的是,可以在不检查自己的情况下点击复选框!
回答by Ssc456
Ok this should do what you want to do, either onClick or on CheckChanged but the answer is from CheckChanged.
好的,这应该做你想做的事情,无论是 onClick 还是 CheckChanged,但答案来自 CheckChanged。
Put this in the chk_CheckChanged event and add the chk_CheckChanged event to each Checkbox you add.
将其放在 chk_CheckChanged 事件中,并将 chk_CheckChanged 事件添加到您添加的每个复选框中。
CheckBox tmp = (CheckBox)sender;
foreach (CheckBox c in flowLayoutPanel1.Controls)
{
c.CheckedChanged -= chk_CheckedChanged;
c.Checked = false;
}
tmp.Checked = true;
foreach (CheckBox c in flowLayoutPanel1.Controls)
{
c.CheckedChanged += chk_CheckedChanged;
}
回答by Christopher Biagtan
- Put the checkbox inside the groupbox control.
- Loop the control enclosed on the groupbox
- Find the checkbox name in the loop, if not match make the checkbox unchecked else checked the box.
- 将复选框放在 groupbox 控件中。
- 循环包含在 groupbox 上的控件
- 在循环中找到复选框名称,如果不匹配,则取消选中复选框,否则选中该框。
See Sample code:
请参阅示例代码:
//Event CheckedChanged of checkbox:
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
if (cb.CheckState == CheckState.Checked)
{
checkboxSelect(cb.Name);
}
}
//Function that will check the state of all checkbox inside the groupbox
private void checkboxSelect(string selectedCB)
{
foreach (Control ctrl in groupBox1.Controls)
{
if (ctrl.Name != selectedCB)
{
CheckBox cb = (CheckBox)ctrl;
cb.Checked = false;
}
}
}