Clear all radio buttons in a page

前端 未结 5 510
误落风尘
误落风尘 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:42

    Check this:

    private void button1_Click(object sender, EventArgs e) 
    {
        var cntls = GetAll(this, typeof(RadioButton));
        foreach (Control cntrl in cntls)
        {
            RadioButton _rb = (RadioButton)cntrl;
            if (_rb.Checked)
            {
                _rb.Checked = false;
            }
        }
    }
    
    public IEnumerable GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast();
        return controls.SelectMany(ctrls => GetAll(ctrls, type)).Concat(controls).Where(c => c.GetType() == type);
    }
    

提交回复
热议问题