【问题标题】:Why does Autosizing a GroupBox and its RadioButton Children not reduce the GroupBox's width when the RadioButton text values are short?为什么当 RadioButton 文本值很短时,自动调整 GroupBox 及其 RadioButton Children 的大小不会减小 GroupBox 的宽度?
【发布时间】: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


    【解决方案1】:

    这行得通:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 2014-12-21
      • 2014-07-19
      • 2017-03-24
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多