Click here to Skip to main content
15,885,855 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm playing around with creating controls at runtime, just for fun.

I used this to drop a usercontrol on a form. The user control has a Label, a RadioButton, and a TextBox. No biggie.

int ControlsOnPanel = 0;
Panel pnl = new Panel();
pnl.Left = 5;
pnl.Top = ControlsOnPage * 21;
pnl.BorderStyle = BorderStyle.None;
pnl.BackColor = Color.AliceBlue;
for (int i = 1; i <= numRadioButtons.Value; i++)
{
    ControlsOnPage += 1;
    ControlsOnPanel += 1;
    EditableRadioButton rb = new EditableRadioButton();
    rb.Margin = new Padding(7);
    rb.RadioButtonText = "Type text here";
    rb.RadioButtonLabel = ControlsOnPage.ToString() + ".";
    rb.Top = (ControlsOnPanel * 21);
    rb.Left = 0;
    rb.Parent = pnl;
    pnl.Controls.Add(rb);
    pnl.Height = rb.Top+21;
}
pnlTarget.Controls.Add(pnl);


and it adds the EditableRadioButtons to the Panel (pnl) just fine, then puts that panel inside another one.

But the RadioButtons don't act like RadioButtons. I can click all of them and they stay clicked.

What did I miss? Or is it because the RadioButton is inside a UserControl and therefore is already exclusive within that control, and dropping it on a Panel with others won't make a bit of difference? :(
Posted
Updated 10-Feb-15 10:00am
v2
Comments
PIEBALDconsult 10-Feb-15 15:30pm    
Fix the title?
I think you're correct about the User Control causing the trouble.
GenJerDan 10-Feb-15 16:02pm    
D'oh! Yeah. Got a CheckBox UserControl, too. :p

And, yeah, I think it probably is the UserControl gobbling that part of it. Well, no matter, since this isn't a "real" thing. Might be fun to figure out how to "reexpose" it.

1 solution

To have some set of radio buttons mutually exclusive, you have to put them all in the same container control, such as GroupBox, Panel or TabPage. The parts of these sets should not have any different Parent control, they all should have the same parent.
If you have another set of radio buttons, also mutually exclusive, but independent from another one, they have to have a parent container.

—SA
 
Share this answer
 

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