2016-05-31 26 views
1

爲什麼tableLayoutPanel.Dock = DockStyle.Fill;不工作並且tableLayoutPanel未填滿Form中的所有可用空間?TableLayoutPanel內部的比例控制

如何縮放button.Text = "Button";

namespace Scalability 
{ 
    static class Program 
    { 
     /// <summary> 
     /// Главная точка входа для приложения. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      ViewForm viewForm = new ViewForm(); 
      Application.Run(viewForm); 
     } 
    } 
} 

namespace Scalability.Forms 
{ 
    class ViewForm:Form 
    { 
     public ViewForm() 
     { 
      TableLayoutPanel tableLayoutPanel = new TableLayoutPanel(); 
      Button button = new Button(); 
      button.Text = "Button"; 
      button.Dock = DockStyle.Fill; 

      Label label = new Label(); 
      label.Text="Label"; 
      label.Dock = DockStyle.Fill; 

      TextBox textBox = new TextBox(); 
      textBox.Text = "textBox"; 
      textBox.Dock = DockStyle.Fill; 

      tableLayoutPanel.Controls.Add(button, 0, 0); 
      tableLayoutPanel.Controls.Add(label, 0, 1); 
      tableLayoutPanel.Controls.Add(textBox, 1, 0); 
      tableLayoutPanel.Dock = DockStyle.Fill; 

      this.Controls.Add(tableLayoutPanel); 
     } 
    } 
} 
+0

你懷疑你的代碼?嘗試在設計器中創建表單,如果它的行爲正確 - 將代碼與'InitializeComponents()'中生成的代碼進行比較。否則,我們需要一張圖片來看看「不起作用」。 – Sinatr

回答

0

configurate的TableLayoutPanelRowStyles,使它們伸展

TableLayoutPanel tableLayoutPanel = new TableLayoutPanel(); 
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50)); 
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50)); 
0

但它的工作。 :)

1)嘗試將顏色附加到像這樣的控件。

Button button = new Button(); 
button.Text = "Button"; 
button.BackColor = Color.Orange; 
button.Dock = DockStyle.Fill; 

Label label = new Label(); 
label.Text = "Label"; 
label.BackColor = Color.Yellow; 
label.Dock = DockStyle.Fill; 

TextBox textBox = new TextBox(); 
textBox.Text = "textBox"; 
textBox.BackColor = Color.Green; 
textBox.Dock = DockStyle.Fill; 

現在您將看到控件完全按照您放置它們的方式使用整個窗口。所以按鈕是col 0,row 0(left,top)。文本框是第1行,第0行(右側,頂部)。標籤爲第0行,第1行(左下角)。而第1行第1行沒有任何內容(右下方的「中心位置」爲空)。

如果向1,1添加一個按鈕,它將在剩餘空間中伸展。像這樣

// Blue button. 
Button bbutton = new Button(); 
bbutton.Text = "Button"; 
bbutton.BackColor = Color.Blue; 
bbutton.Dock = DockStyle.Fill; 

tableLayoutPanel.Controls.Add(button, 0, 0); 
tableLayoutPanel.Controls.Add(label, 0, 1); 
tableLayoutPanel.Controls.Add(textBox, 1, 0); 
// We added this one. 
tableLayoutPanel.Controls.Add(bbutton, 1, 1); 
tableLayoutPanel.Dock = DockStyle.Fill; 

所以你有它。