locked
how to uncheck a dynamically created radio button contained in a group box (winform)? --or-- how to cast a control as a radiobutton? RRS feed

  • Question

  • I create a list of radio buttons dynamically inside of a groupbox (winform). The list of dynamically created radiobuttons is of removeable drives on my computer that are "ready" (like I may have one or two sd cards or thumb drives on one or two USB ports). I select a drive from the list of the dynamically generated radiobuttons. Now I want to uncheck all the radiobuttons. Here's the code I have so far:

    foreach (Control ctl in grpBox1.Controls)
    {
        //if (ctl is RadioButton)
        if (ctl.GetType() == typeof(RadioButton))
        {
           //--pseudocode here -- ctl.Checked = false;           
        }
    }

    the problem I am having is that I can't see a way to cast ctl as a radiobutton so that I can get the Checked property to set to false.  I also tried

    RadioButton rb = (RadioButton)ctl;
    rb.Checked = false;

    but that did not uncheck the radiobuttons in the groupbox.  How can I uncheck "Dynamivally" created radio buttons in the groupbox?


    Rich P



    • Edited by Rich P123 Wednesday, December 30, 2015 11:29 PM ......
    • Moved by Kristin Xie Thursday, December 31, 2015 2:38 AM winfrom related
    Wednesday, December 30, 2015 11:26 PM

Answers

  • Hi Rich P,

    You could reference the following code.

                foreach (Control con in this.groupBox1.Controls)
                {
                    //one way
                    if (con is RadioButton)
                        (con as RadioButton).Checked = false;
                    //another
                    if (con.GetType() == typeof(RadioButton))
                    {
                        RadioButton rb = (RadioButton)con;
                        rb.Checked = false;
                    }
                }
    
                //use control name to find specific control   //control'name //search child control
                Control[] rbtn = this.groupBox1.Controls.Find("radioButton1", true);

    I hope it helps.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 31, 2015 3:20 AM

All replies

  • Nevermind.  I forgot that I created the list of radiobuttons on a panel -- so that I could have a scrollbar inside the groupbox.   Now this code

    RadioButton rb = (RadioButton)ctl;
    rb.Checked = false;

    DOES work when I am looping through the Panel.Controls


    Rich P


    • Edited by Rich P123 Wednesday, December 30, 2015 11:36 PM ....
    Wednesday, December 30, 2015 11:35 PM
  • Nevermind.  I forgot that I created the list of radiobuttons on a panel -- so that I could have a scrollbar inside the groupbox.   Now this code

    RadioButton rb = (RadioButton)ctl;
    rb.Checked = false;

    DOES work when I am looping through the Panel.Controls


    Rich P


    Hi Rich P123,

    This forum is about the C# programming language, IDE, libraries, samples, and tools, as your issue is more related to the Windows Form development and according to your new response, I think you have solved your problem. Thanks for sharing the solution.

    Now, we help you move it to the Windows Forms General forum so that it could better help other community members who have the similar questions. Thank you for your understanding.

    Best Regards,

    Albert Zhang

    Thursday, December 31, 2015 2:34 AM
  • Hi Rich P,

    You could reference the following code.

                foreach (Control con in this.groupBox1.Controls)
                {
                    //one way
                    if (con is RadioButton)
                        (con as RadioButton).Checked = false;
                    //another
                    if (con.GetType() == typeof(RadioButton))
                    {
                        RadioButton rb = (RadioButton)con;
                        rb.Checked = false;
                    }
                }
    
                //use control name to find specific control   //control'name //search child control
                Control[] rbtn = this.groupBox1.Controls.Find("radioButton1", true);

    I hope it helps.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 31, 2015 3:20 AM
  • If you are trying to show which removable drives are ready, You should probably use CheckBox rather than RadioButton. Only one RadioButton in a container can be checked, while any number of Checkboxes can be checked (and any number of removable drives can be ready).
    Thursday, December 31, 2015 3:38 AM