Only one checkbox to be selected

后端 未结 5 1667
被撕碎了的回忆
被撕碎了的回忆 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:48

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

提交回复
热议问题