2012-02-22 120 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