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

TableLayoutPanel isn't showing padding on bottom when scrollbar is visible

我正在尝试为我的 WinForms 应用程序构建 "dashboard" 布局。 我能够使用 TableLayoutPanel 构建简单的概念。 我能够将列、行和控件添加到 tlp (TableLayoutPanel),但我得到了不需要的行为:

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

起初我有 2 行,第一行设置为 100 像素的绝对大小,第二行设置为自动填充,当我单击 "Test" 按钮时,我插入的行与第一行具有相同的属性(绝对大小,100 像素身高)

感谢@Sinatr 和@TaW 我解决了我的问题。


如您所见,在添加更多行并滚动到底部填充后仍然可见(理想效果)。

关键是将 TableLayoutPanel 放在 UserControl 中。

以下是必须设置的属性:

TableLayoutPanel(在用户控件内):

  • 停靠:顶部
  • 自动调整大小:真
  • 自动滚动:假

用户控件:

  • 自动滚动:真

下面是我的 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();
}

我希望这对遇到同样问题的人有所帮助。