Only one checkbox to be selected

后端 未结 5 1668
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 19:15

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 text

5条回答
  •  一生所求
    2021-01-14 19:43

    Try using an external Bool variable to know when the CheckBox was changed "automatically"

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

提交回复
热议问题