简体   繁体   English

C# Winforms 自动调整 GroupBox

[英]C# Winforms autosize GroupBox

My problem is the following: I have a code which gets items from an xml file.我的问题如下:我有一个从 xml 文件中获取项目的代码。 This should be parsed and tabs, groupboxes and labels should be generated accordingly.这应该被解析并相应地生成选项卡、组框和标签。

I have the following code:我有以下代码:

foreach(XmlNode function in root.SelectNodes("function"))
{
    string functionName = function.SelectSingleNode("name").InnerText;
    TabPage tabPage = new TabPage(functionName);
    FlowLayoutPanel fLP_Layout = new FlowLayoutPanel();

    foreach(XmlNode port in function.SelectNodes("Port"))
    {
        string portName = port.SelectSingleNode("name").InnerText;
        GroupBox gB = new GroupBox();
        gB.Text = portName;

        TableLayoutPanel tLP_PortLayout = new TableLayoutPanel();
        tLP_PortLayout.Location = new Point(5, 20);
        tLP_PortLayout.ColumnCount = 2;

        var y = 20;

        foreach(XmlNode register in port.SelectNodes("Register"))
        {
            Label l_register = new Label();
            l_register.Location = new Point(5, y);
            l_register.Text = register.SelectSingleNode("name").InnerText;
            TextBox tB_Value = new TextBox();
            tB_Value.Location = new Point(50, y - 4);

            tLP_PortLayout.Controls.Add(l_register);
            tLP_PortLayout.Controls.Add(tB_Value);
            y += 20;
        }

        gB.Controls.Add(tLP_PortLayout);
        fLP_Layout.Controls.Add(gB)
    }

    tabPage.Controls.Add(fLP_Layout);
    tabControl1.TabPages.Add(tabPage);
}

And well, as you can imagine it doesn't work.而且,正如您可以想象的那样,它不起作用。 The groupbox always has the default size and does not resize according to the content.组框始终具有默认大小,并且不会根据内容调整大小。 Neither does the TableLayoutPanel inside the groupbox.组框内的 TableLayoutPanel 也没有。

setting环境

tLP_PortLayout.AutoSize = true;

and

gB.AutoSize = true

solved the issue for me.为我解决了这个问题。 Thanks @Loathing.谢谢@Loathing。

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

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