【问题标题】:Autosize panel containing tableLayoutPanel自动调整包含 tableLayoutPanel 的面板
【发布时间】:2017-03-08 16:43:26
【问题描述】:

我有一个包含面板的元素:

this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tableLayoutPanel1.SetColumnSpan(this.panel1, 2);
this.panel1.Controls.Add(this.tableLayoutPanel2);
this.panel1.Location = new System.Drawing.Point(10, 10);
this.panel1.Margin = new System.Windows.Forms.Padding(10);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(264, 100);
this.panel1.TabIndex = 2;

此面板包含一个 tableLayoutPanel。 tableLayoutPanel 有 2 行自动调整大小,包含标签/按钮:

this.tableLayoutPanel2.ColumnCount = 2;
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel2.Controls.Add(this.button2, 1, 0);
this.tableLayoutPanel2.Controls.Add(this.button3, 1, 1);
this.tableLayoutPanel2.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.label2, 0, 1);
this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel2.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel2.Name = "tableLayoutPanel2";
this.tableLayoutPanel2.RowCount = 2;
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel2.Size = new System.Drawing.Size(262, 98);
this.tableLayoutPanel2.TabIndex = 0;

行的高度大小正确。我希望面板自动设置他的高度(我会在执行过程中隐藏一些行)。其实我有:

当我为 panel1 设置 autoSize = true 时,我有:

我该怎么做才能拥有这个?

如果我在执行期间添加/删除行,则必须更新面板大小。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    看看“Control.SetBoundsCore”能不能帮到你。您必须创建自己的面板才能使用它。

    您的问题的原因似乎是以下代码行

    this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
    

    【讨论】:

    • 它有效,谢谢。我只需要为面板和布局设置 autosize = true 。当我删除一行时,我将高度强制为 0,并且自动调整大小非常适合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多