2012-12-24 72 views
3

我想在TableLayoutPanel中動態地在GUI上的固定區域添加行。所以,如果記錄數量增加,那麼我想要一個垂直滾動條,這將有助於用戶查看更多記錄。爲此,我設置了Property AutoScroll = true;,但它不起作用。TableLayoutPanel的自動滾動屬性不起作用

CheckBox c = new CheckBox(); 
c.Text = "Han"; 
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows; 
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); 
this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c, 0, 0); 
tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Maximum); 
this.tableLayoutPanel1.AutoScroll = true; 
tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0); 

回答

6

從另一個問題的註釋看你的代碼,你似乎在每一行都添加了行式,嘗試添加行而不添加樣式或先添加一個樣式然後添加所有行。

tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows; 
      tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); 

      this.tableLayoutPanel1.Controls.Add(c); 
      this.tableLayoutPanel1.Controls.Add(c1); 
      this.tableLayoutPanel1.Controls.Add(c2); 
tableLayoutPanel1.VerticalScroll.Maximum = 200; 
      this.tableLayoutPanel1.AutoScroll = true; 
+0

我不得不爲每一行使用不同的樣式,所以不能添加樣式。儘管在最後添加沒有樣式的行解決了這個問題 – Breeze

5

因此,您沒有發佈您的代碼,我不能說你做錯了什麼。但是,這是你應該添加控件到您的表佈局面板的方式:

tableLayoutPanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows; 
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize)); 
tableLayoutPanel.RowCount = tableLayoutPanel.RowStyles.Count; 
YourCountrol control = new YourControl(); 
// setup your control properties 
tableLayoutPanel.Controls.Add(control); 
// scroll to the bottom to see just added control 
tableLayoutPanel.AutoScrollPosition = 
    new Point(0, tableLayoutPanel.VerticalScroll.Maximum); 

當然,你應該有tableLayoutPanel.AutoScroll = true

BTW,以避免惱人的水平滾動條,你應該正確的填充添加到您的表佈局面板:

tableLayoutPanel.Padding = 
    new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0); 

UPDATE AutoSize應該禁用tableLayoutPanel。否則,不會出現滾動 - 表格佈局面板將增長。

+0

這是我的代碼,它仍然不工作: CheckBox c = new CheckBox(); c.Text =「Han」; tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows; tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c,0,0); tableLayoutPanel1.AutoScrollPosition = new Point(0,tableLayoutPanel1.VerticalScroll.Maximum); this.tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.Padding = new Padding(0,0,SystemInformation.VerticalScrollBarWidth,0);' –

+0

'this.tableLayoutPanel1.RowCount = 1;'您將行數設置爲1。那就是問題所在。在設計器中也將自動滾動設置爲true。 –

+0

我也在設計器屬性中設置了自動滾動= true,同時也刪除了'this.tableLayoutPanel1.RowCount = 1;'行但仍然不起作用 –