Only one checkbox to be selected

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

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

提交回复
热议问题