2012-02-22 127 views
16

我有兩行兩列。我希望兩個單元格的最後一列合併成一個。由於要求,我不使用其他設計選項意味着兩個tablelayouts其中第一個表格佈局有兩行。我在C#中使用Winforms。如何合併表格佈局中的兩個單元格

|      |     | 
|      |     | 
|      |     | 
|_______________________|     | 
|      |     | 
|      |     | 
|      |     | 
+0

您正在使用哪個表(類)? – 2012-02-22 07:28:59

+0

您使用了c#標籤。與c#有關嗎?我無法理解。你想要做什麼? – sinanakyazici 2012-02-22 07:30:20

+1

我想設計在Winforms中使用TableLayout的用戶界面 – 2012-02-22 07:32:18

回答

0

在將在表中開始合併的單元格中設置控件的RowSpan屬性。即3的RowSpan將具有控制填充其單元格和下面的2個單元格。

ColumnSpan合併到右側。

在代碼中,調用SetRowSpan和/或SetColumnSpan方法。

6

這裏是如何做到這一點的代碼

//create a label control, add it to the tableLayoutPanel, and merge it into 3 cells. 
Label lbl = new Label(); 
lbl.Location = new Point(0, 0); 
lbl.Text = "This is a test label"; 
MyTableLayoutPanel.Controls.Add(lbl, 0,0); //start it in cell 0,0 
MyTableLayoutPanel.SetColumnSpan(lbl, 3); //merge 3 columns 
1

而不是設置ColumnSpan/RowSpan財產的,可以另一種TableLayoutPanel細胞內添加TableLayoutPanel。不是合併兩個單元格,而是分割兩個單元格。在你提供的例子中,你會將左列分成兩行,而不是將右列合併成一行。

如果您打算將CellBorderStyle屬性設置爲「None」以外的其他屬性,此方法纔有優勢。我發現這個答案here,其中CSharpFreak也建議另一種方法,我沒有嘗試。

0

您可以設置這樣的「合併」屬性來控制:

比方說,控制是一個Label和要合併行,那麼你就可以做到這一點如下:

TableLayoutPanel table = new TableLayoutPanel(); 

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

table.Controls.Add(lbl, 0, 0); //initial position 
table.SetRowSpan(lbl,2); 
0

的下面的代碼應該讓你跨越行的所需數量跨越控制/列

TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel(); // not required if you already have the control added else where or in designer. 
TextBox textBox1 = new TextBox(); // not required if you already have the control added else where or in designer. 
tableLayoutPanel1.Controls.Add(textBox1);// not required if you already have the control added else where or in designer. 
tableLayoutPanel1.SetColumnSpan(textBox1, 2); 
tableLayoutPanel1.SetRowSpan(textBox1, 2); 
13
  1. 將任何控制到細胞中的表單設計
  2. 選擇控制和查看其屬性
  3. 查找「ColumnSpan」中的「佈局」部分爲這個值屬性
  4. 輸入所需的列跨度

見圖片的插圖:

enter image description here