2

我想一次只选择一个复选框。我的程序从文本文件中读取并根据文本文件中有多少“答案”创建复选框。

有谁知道代码有什么问题?

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; }
                }
            }
        }
    }
4

6 回答 6

10

实现你想要的很简单,但它也很奇怪

//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;
}
于 2013-10-14T10:50:52.800 回答
7

我认为您正在寻找单选按钮。

如果您坚持使用复选框,请将您的事件更改为 CheckedChanged,因为这样会更准确。不幸的是,复选框可以在不检查自身的情况下被单击!

于 2013-10-14T10:43:17.167 回答
0

好的,这应该做你想做的事,无论是 onClick 还是 CheckChanged,但答案来自 CheckChanged。

将其放入 chk_CheckChanged 事件中,并将 chk_CheckChanged 事件添加到您添加的每个 Checkbox 中。

        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;
        }
于 2013-10-14T10:54:02.847 回答
0

尝试使用外部 Bool 变量来了解 CheckBox 何时“自动”更改

CheckBox checkBox1;
CheckBox checkBox2;
Bool changed = false; //This one
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!changed)
            {
                if (checkBox2.Checked)
                {
                    changed = true;
                    checkBox2.Checked = false;
                }
            }
            else
            {
                changed = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (!changed)
            {
                if (checkBox1.Checked)
                {
                    changed = true;
                    checkBox1.Checked = false;
                }
            }
            else
            {
                changed = false;
            }
        }
于 2020-10-11T16:29:27.283 回答
0
  1. 将复选框放在 groupbox 控件内。
  2. 循环包含在 groupbox 上的控件
  3. 在循环中查找复选框名称,如果不匹配,则取消选中复选框,否则选中该框。

请参阅示例代码:

//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;
        }
    }
}
于 2016-12-06T08:38:48.133 回答
0

添加一个checkedListBox并试试这个

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = checkedListBox1.SelectedIndex;
        int count = checkedListBox1.Items.Count;
        for(int i = 0; i < count; i++)
        {
            if(index != i)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
        }
    }
于 2022-02-02T14:56:05.733 回答