簡體   English   中英

C# Winforms 自動調整 GroupBox

[英]C# Winforms autosize GroupBox

我的問題如下:我有一個從 xml 文件中獲取項目的代碼。 這應該被解析並相應地生成選項卡、組框和標簽。

我有以下代碼:

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);
}

而且,正如您可以想象的那樣,它不起作用。 組框始終具有默認大小,並且不會根據內容調整大小。 組框內的 TableLayoutPanel 也沒有。

環境

tLP_PortLayout.AutoSize = true;

gB.AutoSize = true

為我解決了這個問題。 謝謝@Loathing。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM