0
votes

I'm dynamically creating some controls; using the AutoSize property helps keep them in check, width-wise. But in the case of a GroupBox, doing this:

private GroupBox getGroupBox(string currentSettings, int curLeftVal)
{
    // Adapted from Pierre's answer at https://stackguides.com/questions/23944419/why-is-only-the-first-radiobutton-being-added-to-the-groupbox
    IList<string> grpbxVals = new List<string>(currentSettings.Split('~'));
    GroupBox gb = new GroupBox { Height = 60, Location = new Point(curLeftVal, 0) };

    gb.AutoSize = true;
    int radButtonYVal = 0;
    int leftVal = 0;
    for (int i = 0; i < grpbxVals.Count() - 1; i++)
    {
        gb.Controls.Add(new RadioButton { Text = grpbxVals[i], AutoSize = true, Location = new Point(gb.Location.X + 2, radButtonYVal) });
        radButtonYVal += new RadioButton().Height - 4; // the "-4" is a kludge to scrunch the radiobuttons together a bit
    }
    return gb;
}

...with both the groupbox and the child radio buttons does not seem to work.

Even when the radio button text values are quite short, the group box is wider than it needs to be:

enter image description here

Why?

UPDATE

If I change the type of container form a Panel) to a FlowLayoutPanel, the first groupbox looks good, but subsequent ones are still goofed up:

enter image description here

Note: A related post is here; I still don't know how to adjust the left value for subsequent Group Box's children (in my case, Radio Buttons).

1

1 Answers

0
votes

This works:

int radButtonYVal = 4;
int leftVal = 4;
for (int i = 0; i < grpbxVals.Count() - 1; i++)
{
    gb.Controls.Add(new RadioButton { Text = grpbxVals[i], AutoSize = true, Location = new Point(leftVal, radButtonYVal) });
    radButtonYVal += new RadioButton().Height -4; // the "-4" is a kludge to scrunch the radiobuttons together a bit
}