29

我有两行两列。我希望两个单元格的最后一列合并为一个。由于要求,我不使用其他设计选项意味着两个表格布局,其中第一个表格布局有两行。我在 C# 中使用 Winforms。

|                       |                    |
|                       |                    |
|                       |                    |
|_______________________|                    |
|                       |                    |
|                       |                    |
|                       |                    |
4

7 回答 7

44
  1. 将任何控件放入表单设计器中的单元格中
  2. 选择控件并查看其属性
  3. 在“布局”部分中找到“ColumnSpan”属性
  4. 为此值输入所需的列跨度

见图:

在此处输入图像描述

于 2016-03-31T10:10:39.197 回答
8

这是在代码中执行此操作的方法

//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
于 2014-03-19T23:36:57.740 回答
5

http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx

例如,您可以在 TableLayoutPanel 控件中设置 RowSpan 属性。

于 2012-02-22T07:48:23.363 回答
1

您可以在另一个TableLayoutPanel的单元格中添加一个TableLayoutPanel ,而不是设置ColumnSpan / RowSpan属性。不是合并两个单元格,而是拆分两个单元格。在您在问题中提供的示例中,您会将左列拆分为两行,而不是将右列合并为一行。

仅当您计划将CellBorderStyle属性设置为“”以外的其他值时,此方法才有优势。我在这里找到了这个答案,其中CSharpFreak还建议了另一种方法,我没有尝试过。

于 2014-05-02T14:43:05.383 回答
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);
于 2015-02-24T22:28:18.657 回答
0

在将在表格中开始合并的单元格中设置控件的 RowSpan 属性。即 3 的 RowSpan 将使控件填充其单元格和下面的 2 个单元格。

要合并到右侧的 ColumnSpan。

在代码中,调用 SetRowSpan 和/或 SetColumnSpan 方法。

于 2013-12-10T20:10:30.110 回答
0

您可以将此类“合并”属性设置为控件:

假设 Control 是 aLabel并且您想要合并行,那么您可以按以下方式进行操作:

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);
于 2014-09-25T07:54:50.470 回答