Only one checkbox to be selected

Deadly 提交于 2019-12-19 10:51:29

问题


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

回答1:


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



回答2:


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!




回答3:


Ok this should do what you want to do, either onClick or on CheckChanged but the answer is from CheckChanged.

Put this in the chk_CheckChanged event and add the chk_CheckChanged event to each Checkbox you add.

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



回答4:


  1. Put the checkbox inside the groupbox control.
  2. Loop the control enclosed on the groupbox
  3. Find the checkbox name in the loop, if not match make the checkbox unchecked else checked the box.

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


来源:https://stackoverflow.com/questions/19358419/only-one-checkbox-to-be-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!