当滚动条可见时,TableLayoutPanel底部的填充不显示

4
我正在尝试为我的WinForms应用程序构建类似“仪表板”的布局。我使用TableLayoutPanel构建了简单的概念。我能够向tlp(TableLayoutPanel)添加列、行和控件,但是我得到了不想要的行为:
enter image description here
我在tlp上设置了填充为5,但是在添加两行后,该填充消失了 - 我可以滚动到底部,但是当滚动条在底部时,我希望填充可见。
我使用以下代码添加新行:
private void newButton_Click(object sender, EventArgs e)
{
    var x = tableLayoutPanel1.RowStyles.Count - 1;
    tableLayoutPanel1.RowStyles.Insert(x,new RowStyle(SizeType.Absolute,100));
    var b = new Button
    {
        Dock = DockStyle.Fill,
        Text = "New"+x.ToString(),
        UseVisualStyleBackColor = true
    };
    tableLayoutPanel1.RowCount++;
    x = tableLayoutPanel1.RowStyles.Count - 2;
    tableLayoutPanel1.Controls.Add(b,0,x);
}

这种行为是设计上的还是可以更改的?基本上,当我滚动到tlp底部时,我希望底部填充区域可见。
如请求所示,这是我的tlp设计代码:
private void InitializeComponent()
{
    this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button5 = new System.Windows.Forms.Button();
    this.tableLayoutPanel1.SuspendLayout();
    this.SuspendLayout();
    // 
    // tableLayoutPanel1
    // 
    this.tableLayoutPanel1.AutoScroll = true;
    this.tableLayoutPanel1.AutoScrollMargin = new System.Drawing.Size(10, 10);
    this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
    this.tableLayoutPanel1.ColumnCount = 2;
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
    this.tableLayoutPanel1.Controls.Add(this.button1, 0, 0);
    this.tableLayoutPanel1.Controls.Add(this.button2, 1, 0);
    this.tableLayoutPanel1.Cursor = System.Windows.Forms.Cursors.Default;
    this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 12);
    this.tableLayoutPanel1.Name = "tableLayoutPanel1";
    this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(5);
    this.tableLayoutPanel1.RowCount = 2;
    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
    this.tableLayoutPanel1.Size = new System.Drawing.Size(459, 221);
    this.tableLayoutPanel1.TabIndex = 0;
    // 
    // button1 - button in top left cell in tlp
    // 
    this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.button1.Location = new System.Drawing.Point(8, 8);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(218, 94);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    // 
    // button2 - button in top right cell in tlp
    // 
    this.button2.Dock = System.Windows.Forms.DockStyle.Fill;
    this.button2.Location = new System.Drawing.Point(232, 8);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(219, 94);
    this.button2.TabIndex = 1;
    this.button2.Text = "button2";
    this.button2.UseVisualStyleBackColor = true;
    // 
    // button5 - "Test" button
    // 
    this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
    this.button5.Location = new System.Drawing.Point(12, 427);
    this.button5.Name = "button5";
    this.button5.Size = new System.Drawing.Size(75, 23);
    this.button5.TabIndex = 1;
    this.button5.Text = "Test";
    this.button5.UseVisualStyleBackColor = true;
    this.button5.Click += new System.EventHandler(this.button5_Click);
    // 
    // Form2
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(755, 462);
    this.Controls.Add(this.tableLayoutPanel1);
    this.Controls.Add(this.button5);
    this.Name = "Form2";
    this.Text = "Form2";
    this.Load += new System.EventHandler(this.Form2_Load);
    this.tableLayoutPanel1.ResumeLayout(false);
    this.ResumeLayout(false);
}

起初我有两行,第一行设置为100像素的绝对大小,第二行设置为自动填充。当我点击“测试”按钮时,我会插入一行,该行具有与第一行相同的属性(绝对大小,高度为100像素)。

你能展示一下你的 TableLayoutPanel 的设计代码吗? - Zein Makki
我无法复现。这里运行得很好。请注意,距离是按钮的边距。你的TLP是否开启了AutoScroll? - TaW
啊,你应该将AutoScrollMargin设置为0! - TaW
2
或者相反,它在那里,但是 ScrollBar 没有考虑到它。尝试将 TableLayoutPanel(禁用滚动条)托管在 Panel(启用滚动条)内,看看会发生什么。 - Sinatr
1
对于这个解决方法,我会建议将AutoSize设置为true。 - TaW
显示剩余11条评论
1个回答

3

多亏了@Sinatr和@TaW的帮助,我解决了我的问题。

enter image description here
您可能已经注意到,在添加更多行并向底部滚动后,仍然可以看到填充(这是期望的效果)。

关键是将TableLayoutPanel放在UserControl中。

以下是必须设置的属性:

TableLayoutPanel(在UserControl内部):

  • Dock:Top
  • AutoSize:True
  • AutoScroll:False

UserControl:

  • AutoScroll:True

下面是来自我的UserControl的设计器代码:

private void InitializeComponent()
{
    this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
    this.SuspendLayout();
    // 
    // tableLayoutPanel1
    // 
    this.tableLayoutPanel1.AutoSize = true;
    this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
    this.tableLayoutPanel1.ColumnCount = 3;
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.34F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33F));
    this.tableLayoutPanel1.Cursor = System.Windows.Forms.Cursors.Default;
    this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
    this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
    this.tableLayoutPanel1.Name = "tableLayoutPanel1";
    this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(5);
    this.tableLayoutPanel1.RowCount = 1;
    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
    this.tableLayoutPanel1.Size = new System.Drawing.Size(443, 10);
    this.tableLayoutPanel1.TabIndex = 1;
    // 
    // Dashboard
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.AutoScroll = true;
    this.BackColor = System.Drawing.Color.Transparent;
    this.Controls.Add(this.tableLayoutPanel1);
    this.Name = "Dashboard";
    this.Size = new System.Drawing.Size(443, 208);
    this.Load += new System.EventHandler(this.Dashboard_Load);
    this.MouseEnter += new System.EventHandler(this.Dashboard_MouseEnter);
    this.ResumeLayout(false);
    this.PerformLayout();
}

我希望这能帮助到遇到同样问题的任何人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接