Grouping Windows Forms Radiobuttons with different parent controls in C#

前端 未结 4 1062
再見小時候
再見小時候 2021-01-11 19:30

I\'ve got a Windows Forms application in which I have a number of RadioButtons. These RadioButtons are placed within a FlowLayoutPanel which automatically arranges

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-11 20:19

    I'm afraid you'll have to handle this manually... It's not so bad actually, you can probably just store all the RadioButton in a list, and use a single event handler for all of them:

    private List _radioButtonGroup = new List();
    private void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rb = (RadioButton)sender;
        if (rb.Checked)
        {
            foreach(RadioButton other in _radioButtonGroup)
            {
                if (other == rb)
                {
                    continue;
                }
                other.Checked = false;
            }
        }
    }
    

提交回复
热议问题