Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, i have a question. If i have 2 groupboxes with radiobuttons in both, how can i make that i can mark one radiobutton in each groupbox? Cause if i now do that i can only have one.. :s
Posted
Comments
CHill60 3-May-13 10:14am    
Standard behaviour for radiobuttons within a groupbox (or a panel) is to only consider the radiobuttons within that same groupbox. Are you absolutely sure that you have placed the radiobuttons into the groupboxes and not just onto the form?

Please see the comment to the question by CHill60.

The problem should be already solved: two separate group boxes for the two groups. You can use any class of parent controls for those radio buttons, such as panels.

—SA
 
Share this answer
 
RadioButtons use the Parent property to check for exclusivity. When two RadioButtons share the same parent, they cannot both be checked at the same time (even programmatically) but when they have different Parents, like when they are placed in different containers (GroupBoxes, Panels, etc), they can be checked at the same time.

However, if you just wanted to disable a button on another container, you could manipulate the Checked Property programmatically to achieve this. Like so,

C#
RadioButton_checkChanged(Object sender, EventArgs e)
{
    if (RadioButton1.Checked)
        RadioButton2.Checked = false;
}


This method however could get tedious if there are so many RadioButtons to manipulate. This is a workaround I used sometime ago for something else, only just modified it to use the Tag Property of the RadioButton to check or uncheck a RadioButton.

You can download the source here.

C#
//First we want to be able to find all RadioButtons on a Form and in all containers.
//The RadioButtons will then be added to a Collection so that they can be manipulated later.
List<radiobuttons> AllRadios = new List<radiobuttons>();
void FindRadios(Control container)
        {
            foreach (Control x in container.Controls) //Iterate through all the Controls in the Container
            {
                if (x.HasChildren) //Is this Control a container containing other Controls?
                {
                    FindRadios(x); //Find all Controls on the Container
                }

                if (x is RadioButton) //Is this Control a RadioButton?
                {
                    AllRadios.Add(x as RadioButton); //Add the RadioButton to the list.
                    ((RadioButton)x).Click += BindRadiosEvent; //Bind the Click event of the RadioButton to a single one.
                }
            }
            
        }
// Now, the Checked Property will be compared in a BindRadiosEvent using the Tag Property of each RadioButton.
void BindRadiosEvent(object sender, EventArgs e)
        {
            RadioButton clicked = sender as RadioButton;
            if (clicked.Tag == null) return; //If the Tag property of the RadioButton is blank, leave.
            AllRadios.ForEach( //Find all RadioButtons with the same tag.
                    delegate(RadioButton x)
                    {
                        if (x != clicked && x.Tag == clicked.Tag)
                        {                                                        
                            x.Checked = false;
                            clicked.Checked = true;
                        }
                    }
                );
        }


Now, by setting the Tag property of two RadioButtons to the same thing. They will act as if they were on the same Container.

Breakout ILSpy to see how Microsoft did theirs.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900