2011-02-08 16 views
0


我有一個TableLayoutPanel動態添加行。
每一行都有一個絕對大小。
我將AutoScroll設置爲「true」,但是當我添加超出TableLayoutPanel顯示的行時,我看不到滾動。TableLayoutPanel和AutoScroll

這是設計師代碼:

namespace WindowsFormsApplication1 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      // 
      // tableLayoutPanel1 
      // 
      this.tableLayoutPanel1.AutoScroll = true; 
      this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single; 
      this.tableLayoutPanel1.ColumnCount = 1; 
      this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); 
      this.tableLayoutPanel1.Location = new System.Drawing.Point(24, 48); 
      this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 
      this.tableLayoutPanel1.RowCount = 1; 
      this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); 
      this.tableLayoutPanel1.Size = new System.Drawing.Size(239, 163); 
      this.tableLayoutPanel1.TabIndex = 0; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(24, 10); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(239, 28); 
      this.button1.TabIndex = 1; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(292, 266); 
      this.Controls.Add(this.button1); 
      this.Controls.Add(this.tableLayoutPanel1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 
     private System.Windows.Forms.Button button1; 
    } 
} 

,這是我怎麼加行:

using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      tableLayoutPanel1.RowCount++; 
      tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30)); 
      Button b = new Button(); 
      tableLayoutPanel1.Controls.Add(b, 0, tableLayoutPanel1.RowCount - 1); 

     } 
    } 
} 

有誰知道這是怎麼回事?
謝謝:)

回答

1

將AutoScroll屬性設置爲True。

確保設計人員添加的默認行不會導致任何問題。讓你的構造函數看起來像這樣:

public Form1() { 
     InitializeComponent(); 
     tableLayoutPanel1.RowCount = 0; 
     tableLayoutPanel1.RowStyles.Clear(); 
     tableLayoutPanel1.AutoScroll = true; 
    } 
2

看起來像你缺少tableLayoutPanel.AutoSize = true。

+0

感謝這個額外的評論。它幫助了我。事實上,如果你想讓一個tablelayoutpanel在一個winform中滾動,你需要將autoscroll和autosize都設置爲true,並且-off course--確保tablelayoutpanel中的行的組合高度能夠使表單的高度變化。 – 2012-06-13 10:00:07

0

嘗試下面的線,可以爲你工作:

this.tableLayoutPanel1.AutoScroll = true; 
this.tableLayoutPanel1.Dock = DockStyle.Top;