Brandon Gorcheff
Brandon Gorcheff

Reputation: 13

dynamically created radio buttons not displaying

I have an application in which there are several tabs. One of those creates several group boxes and in each of these group boxes I need 10 radio buttons ranging from 1-10. My problem is that I cannot get the radio button to show up and work properly. When I create them if I add them to the current tabs controls all the radio buttons will display but the winform treats them all as one set of radio.

I need the radio buttons in each groupbox to be a set. If I add the buttons to the groupbox the radio buttons will not display. I have played around with the order in which I add the radio button to the groupbox, call the radio buttons show() method, add the groupbox to the tabs control, and call the groupbox's show() method but no matter what configuration I try these in I can't seem to get the radio buttons to display. I also tried to change the childIndex of the radio button but that didn't work either.

Some of you may suggest to just use a drop down or upDownNumaric but I actually have the UpDownNumaric working but the customer wants it changed to a set of radio buttons. The code I currently have:

groupBoxLocation.Y += 45;
GroupBox newGroupBox = new GroupBox();

newGroupBox.Location = groupBoxLocation;
newGroupBox.Text = reader["Description"].ToString().Trim();
newGroupBox.Size = new Size(425, 40);
newGroupBox.Name = ("PS_L_" + newGroupBox.Text).Replace(" ", "").Trim();


RadioButton rateValue;


radioButtonsLocation = new Point(newGroupBox.Location.X - 30, newGroupBox.Location.Y + 15);

tabControl1.TabPages[3].Controls.Add(newGroupBox);

newGroupBox.Show();
for (int i = 0; i < 10; ++i)
{
    rateValue = new RadioButton();
    radioButtonsLocation = new Point(radioButtonsLocation.X + 41, radioButtonsLocation.Y);

    rateValue.Location = radioButtonsLocation;
    rateValue.Text = (i + 1).ToString().Trim();
    rateValue.Width = 40;
    rateValue.Name = "PI_V_" + newGroupBox.Text.Replace(" ", "") + "_" + i;

    newGroupBox.Controls.Add(rateValue);
    newGroupBox.Controls[rateValue.Name].Show();

}

Upvotes: 1

Views: 1780

Answers (1)

AWinkle
AWinkle

Reputation: 673

The problem is your initialization of the radioButtonsLocation. The locations are relative to their parent, not relative to the root container, so try changing

radioButtonsLocation = new Point(newGroupBox.Location.X - 30, newGroupBox.Location.Y + 15);

to

radioButtonsLocation = new Point(0,10);

or some similar point based on how you would like your UI to look.

Upvotes: 3

Related Questions