繁体   English   中英

为什么当RadioButton文本值短时,自动调整GroupBox及其RadioButton子级的大小不会减小GroupBox的宽度?

[英]Why does Autosizing a GroupBox and its RadioButton Children not reduce the GroupBox's width when the RadioButton text values are short?

我正在动态创建一些控件; 使用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,则第一个组框看起来不错,但随后的组框仍然存在问题:

在此处输入图片说明

注意 :相关文章在这里 我仍然不知道如何为后续Group Box的子项(在我的情况下为Radio Buttons)调整左值。

这有效:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM