Clear all radio buttons in a page

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

    void Button1Click(object sender, EventArgs e)
    {
        foreach (Control ctrl in Controls)
        {
            if (ctrl is Panel)
            {
                foreach (Control rdb in ctrl.Controls)
                {
                    if (rdb is RadioButton && ((RadioButton)rdb).Checked == true)
                    {
                        ((RadioButton)rdb).Checked = false;
                    }
                }
            }
        }
    }
    

    This clears all the checked radio buttons on a button click.

提交回复
热议问题