【问题标题】:How to merge two cells in Table Layout如何在表格布局中合并两个单元格
【发布时间】:2012-02-22 07:21:38
【问题描述】:

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

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

【问题讨论】:

  • 您使用的是哪个表(类)?
  • 你使用了 c# 标签。与c#有关系吗?我无法理解。您想在哪里执行此操作?
  • 我想设计在 Winforms 中使用 TableLayout 的 UI

标签: c# winforms c#-4.0 tablelayoutpanel


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

见图:

【讨论】:

  • 对于这个问题应该改变控件的RowSpan属性。
  • @Shaho,谢谢。正确的。我捕获了水平方向合并单元格的屏幕,这是我在寻找提示并提出这个问题时的情况。我希望它对任一跨度方向都有帮助。
【解决方案2】:

下面是如何在代码中做到这一点

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        要合并到右侧的ColumnSpan。

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

        【讨论】:

          【解决方案6】:

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

          假设 Control 是 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);
          

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-05-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-02-27
              相关资源
              最近更新 更多