如何在表格布局中合并两个单元格

发布于 2025-01-07 17:41:53 字数 448 浏览 2 评论 0原文

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

|                       |                    |
|                       |                    |
|                       |                    |
|_______________________|                    |
|                       |                    |
|                       |                    |
|                       |                    |

I have two rows and two columns. I want last column of both cells merge into one. Due to requirement I do not use other design options means two tablelayouts in which first table layout has two rows.I am use Winforms in C#.

|                       |                    |
|                       |                    |
|                       |                    |
|_______________________|                    |
|                       |                    |
|                       |                    |
|                       |                    |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挥剑断情 2025-01-14 17:41:53
  1. 将任意控件放入表单设计器中的单元格中
  2. 选择控件并查看其属性
  3. 在“布局”部分中查找“ColumnSpan”属性
  4. 输入该值所需的列跨度

请参阅图片进行说明:

在此处输入图像描述

  1. Put any control into a cell in form designer
  2. Select the control and view its properties
  3. Find "ColumnSpan" property in "Layout" section
  4. Input desired column span for this value

See picture for illustration:

enter image description here

粉红×色少女 2025-01-14 17:41:53

以下是如何在代码中执行此操作

//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

Here's how to do it in code

//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
暖心男生 2025-01-14 17:41:53

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

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

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

For example You can set RowSpan poperty in TableLayoutPanel control.

半枫 2025-01-14 17:41:53

而不是设置 ColumnSpan/< a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.setrowspan.aspx" rel="nofollow">RowSpan 属性,您可以添加 TableLayoutPanel 在另一个 TableLayoutPanel。您不是合并两个单元格,而是拆分两个单元格。在您问题中提供的示例中,您将把左列拆分为两行,而不是将右列合并为一行。

仅当您计划设置 时,此方法才有用CellBorderStyle 属性设置为除“"。我找到了这个答案 此处,其中CSharpFreak 还建议了另一种方法,我没有尝试。

Instead of setting the ColumnSpan/RowSpan property, you can add a TableLayoutPanel within the cell of another TableLayoutPanel. Instead of merging two cells, you are then splitting two cells. In the example you provide in your question, you would be splitting the left column into two rows, instead of merging the right column into one row.

This method is only advantageous if you plan to set the CellBorderStyle property to something other than "None". I found this answer here, where CSharpFreak also suggests another method, which I didn't try.

〗斷ホ乔殘χμё〖 2025-01-14 17:41:53

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

ColumnSpan 向右合并。

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

Set the RowSpan property of the control in the cell that will start the merge in the table. i.e. RowSpan of 3 will have the control fill its cell and the 2 cells below.

ColumnSpan to merge to right.

In code, call the SetRowSpan and/or SetColumnSpan method.

鱼忆七猫命九 2025-01-14 17:41:53

您可以为控件设置这样的“合并”属性:

假设控件是一个标签,并且您想要合并行,那么您可以按如下方式执行操作:

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);

You can set such "merging" property to the Control:

Let's say the Control is a Label and you want to merge rows, then you can do it as following:

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);
失退 2025-01-14 17:41:53

以下代码应允许您将控件跨越所需的行/列数

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);

The following code should allow you to span a control across desired number of rows/columns

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文