我正在动态创建一些控件;使用AutoSize属性有助于在宽度方面检查它们 . 但是在GroupBox的情况下,这样做:
private GroupBox getGroupBox(string currentSettings, int curLeftVal)
{
// Adapted from Pierre's answer at https://stackoverflow.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;
}
...同时使用groupbox和子单选按钮似乎不起作用 .
即使单选按钮文本值很短,组框也要比它需要的宽:
为什么?
更新
如果我将容器的类型从Panel更改为FlowLayoutPanel,则第一个groupbox看起来不错,但后续的仍然是愚蠢的:
Note :相关帖子是here;我仍然不喜欢孩子(在我的情况下,收音机按钮) .
1 回答
这有效: