I am trying to dynamically create Windows controls and add them to a Panel. For Button and Checkbox this has worked fine; I've run into a problem with a GroupBox, though, with RadioButtons inside it.
The first RadioButton element is created and added to the GroupBox in the expected location, but susbequent ones, although ostensibly created (stepping through the code makes that appear to be the case), they are not visible.
I would think that if subsequent RadioButtons were being plopped atop the previous ones, the last one would be the one seen. This is what it looks like:
Each ~-delimited val should be the text value of a radioButton, yet only one displays. Do I need to explicitly provide the Location vals for the subsequent RadioButtons, or why is this failing?
Here is the code:
private GroupBox getGroupBox(string currentSettings, int curLeftVal)
{
// "apple~orange~peach~True (must look for "enclose group in a black box" as the last val (ignore for the quick-and-dirty mockup, though))
List<string> grpbxVals = new List<string>(currentSettings.Split('~'));
GroupBox gb = new GroupBox();
gb.Height = 60;
gb.Location = new Point(curLeftVal, PANEL_TOP_LOC);
RadioButton radbtn = null;
// "-1" because we're ignoring the final bool ("enclose in black box")
for (int i = 0; i < grpbxVals.Count-1; i++)
{
radbtn = new RadioButton();
radbtn.Text = grpbxVals[i];
gb.Controls.Add(radbtn);
}
return gb;
}
UPDATE
The idea in the answer below by Pierre seems sensible, but it's still not quite what the doctor ordered:
UPDATE 2
This works pretty well (modification of Pierre's code):
IList<string> grpbxVals = new List<string>(currentSettings.Split('~'));
GroupBox gb = new GroupBox { Height = 60, Location = new Point(curLeftVal, 0) };
int radButtonPosition = 0;
for (int i = 0; i < grpbxVals.Count() - 1; i++)
{
gb.Controls.Add(new RadioButton { Text = grpbxVals[i], Location = new Point(curLeftVal, radButtonPosition) });
radButtonPosition += new RadioButton().Height - 4; // the "-4" is a kludge
}
return gb;
Gives me: