【问题标题】:Arranging controls in Winforms TableLayoutPanel在 Winforms TableLayoutPanel 中排列控件
【发布时间】:2022-01-25 21:59:43
【问题描述】:

我在运行时使用panel.Controls.Add(control) 向 Winforms TableLayoutPanel 添加控件。

我需要通过先填充第一列然后填充第二列来将控件排列在具有可变行数的两列中,以实现以下布局:

C1 C4
C2 C5
C3 C6

无论我如何配置面板,它总是按以下顺序填充:

C1 C2
C3 C4
C5 C6

如何更改控件插入的顺序?

【问题讨论】:

  • 查看this postthe other one 了解如何在运行时以网格布局排列控件。
  • 如果可能的话当然考虑使用DataGridView。

标签: winforms


【解决方案1】:

使用 tableLayoutPanel1.Controls.Add() 的第二个重载来传递行和列索引: public virtual void Add(Control control, int column, int row);

示例:

private void SetTableLayoutPanel()
{
    tableLayoutPanel1.RowStyles.Clear();
    tableLayoutPanel1.ColumnCount = 2;
    tableLayoutPanel1.RowCount = 3;
    tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

    var counter = 1;
    for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
    {
        for (int j = 0; j < tableLayoutPanel1.RowCount; j++)
        {
            Button b = new Button();
            b.Text = "C" + counter;
            tableLayoutPanel1.Controls.Add(b, i, j);

            counter++;
        }               
    }
}

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    相关资源
    最近更新 更多