【发布时间】:2014-07-22 20:04:51
【问题描述】:
我正在动态创建一些控件;使用 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;
}
...组合框和子单选按钮似乎都不起作用。
即使单选按钮文本值很短,组框也比它需要的宽:
为什么?
更新
如果我将容器的类型从 Panel) 更改为 FlowLayoutPanel,第一个 groupbox 看起来不错,但后续的仍然搞砸了:
注意:相关帖子为here;我仍然不知道如何为后续 Group Box 的子项(在我的情况下为 Radio Buttons)调整 left 值。
【问题讨论】:
标签: c# winforms radio-button groupbox