只能选择一个复选框

时间:2013-10-14 10:40:46

标签: c# winforms checkbox

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

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

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

5 个答案:

答案 0 :(得分:7)

我认为你正在寻找Radio Buttons。

如果您坚持使用复选框,请将您的活动更改为CheckedChanged,因为这样会更准确。不幸的是,点击框可以不自行检查!

答案 1 :(得分:7)

实现你想要的东西是如此简单,但它也是如此奇怪

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

答案 2 :(得分:0)

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

将此信息放入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;
        }

答案 3 :(得分:0)

  1. 将复选框放在groupbox控件中。
  2. 循环包含在组框中的控件
  3. 在循环中找到复选框名称,如果不匹配则取消选中复选框,否则选中该复选框。
  4. 参见示例代码:

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

答案 4 :(得分: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;
            }
        }