Clear all radio buttons in a page

前端 未结 5 609
误落风尘
误落风尘 2021-01-13 13:08

I have lot of dynamically generated radio buttons in my Windows Forms project. They may be checked based on the values in a database. I want to clear all radio buttons in a

5条回答
  •  渐次进展
    2021-01-13 13:37

    Both will do it below, as long as _radioContainer is a GroupBox.

    private void button1_Click(object sender, EventArgs e) {
    
        // This will remove the radiobuttons completely...
        _radioContainer.Controls.OfType().ToList().ForEach(p => _radioContainer.Controls.Remove(p));
    
        // Either of the below will clear the checked state
        _radioContainer.Controls.OfType().ToList().ForEach(p => p.Checked = false);
    
        foreach (RadioButton radio in _radioContainer.Controls.OfType().ToList()) {
            if (radio.Checked == true) {
                radio.Checked = false;
                break;
            }
        }
    }
    

提交回复
热议问题