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,
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.
List<radiobuttons> AllRadios = new List<radiobuttons>();
void FindRadios(Control container)
{
foreach (Control x in container.Controls)
{
if (x.HasChildren)
{
FindRadios(x);
}
if (x is RadioButton)
{
AllRadios.Add(x as RadioButton);
((RadioButton)x).Click += BindRadiosEvent;
}
}
}
void BindRadiosEvent(object sender, EventArgs e)
{
RadioButton clicked = sender as RadioButton;
if (clicked.Tag == null) return;
AllRadios.ForEach(
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.