以编程方式更改行/列跨度(tablelayoutpanel)

发布于 2024-08-30 21:45:08 字数 244 浏览 3 评论 0原文

我有一个表格布局面板。 2x2 - 2 列 2 行。

例如,我在 1 行、第二列中添加了一个按钮 button1button1 的停靠属性设置为 Fill。 VS Designer 允许设置button1的列/行跨度属性。

我希望能够以编程方式更改 button1 的行跨度属性,以便它可以填充所有第二列(1 行和第二行)并可以将其设置回来。

如何?

I have a tablelayoutpanel. 2x2 - 2 columns 2 rows.

For example, I added a button button1 in a 1 row, second column. button1 has a dock property set to Fill. VS Designer allows to set column/row span properties of button1.

I want an availability to change row span property of button1 programatically, so it can fill all second column(1 row and second row) and availability to set it back.

How?

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

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

发布评论

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

评论(2

短暂陪伴 2024-09-06 21:45:08

这段代码怎么样?

private void button1_Click(object sender, EventArgs e)
{
    var control = sender as Control;

    if(control == null)
        return;

    if (1 == tableLayoutPanel1.GetRowSpan(control))
    {
        tableLayoutPanel1.SetRowSpan(control, 2);
    }
    else
    {
        tableLayoutPanel1.SetRowSpan(control, 1);
    }
}

What about this code?

private void button1_Click(object sender, EventArgs e)
{
    var control = sender as Control;

    if(control == null)
        return;

    if (1 == tableLayoutPanel1.GetRowSpan(control))
    {
        tableLayoutPanel1.SetRowSpan(control, 2);
    }
    else
    {
        tableLayoutPanel1.SetRowSpan(control, 1);
    }
}
撕心裂肺的伤痛 2024-09-06 21:45:08

虽然我发现当前投票的答案相当充分,但它也显得比需要的稍微混乱一些。在设置控件的属性之前,必须将控件添加到 tableLayoutPanel。

Visual Studio(2013 和可能的其他版本)会将这些属性显示为控件的一部分。实际上,它们是 tableLayoutPanel 的一部分。

说明:

tableLayoutPanel.Controls.Add(**control**, x, y)
tableLayoutPanel.SetColumnSpan(**control**, '# of cols to span')

示例:

tableLayoutPanel1.Controls.Add(**button1**, 0, 0);
tableLayoutPanel1.SetColumnSpan(**button1**, 2);
tableLayoutPanel1.SetRowSpan(**button1**, 3);

结果:“占据”此空间的按钮。 (只要它足够大以覆盖该区域。即使它没有“覆盖”该空间,它仍然会“保留”它。)

O O X X X
O O X X X
O O X X X
X X X X X
X X X X X

将跨度设置为大于网格的大小将..:
不要改变网格大小。
不要将数字裁剪/编辑为网格的大小。
编译时不抛出错误。

它的行为/执行就像将跨度设置为当前网格(tableLayoutPanel)最大尺寸一样。这仅在 TLP/网格大小发生变化时才相关。

如果以编程方式将两个控件添加到同一网格位置,则网格中的第一个控件将保留其位置。任何随后添加的控件都会被推送到下一个单元格块。如果添加“跨度”,它将将该单元块视为已使用,并继续搜索未使用的单元块。

例如:label1label2label3 添加到 0,0。

  • label1 将出现在 0,0
  • label2: 0,1
  • label3: 0,2

Ex 2: 标签 1 的行跨度为 2。

  • label1: 0,0
  • label2: 重新定位到 0,2
  • label3: 0,3

选择正确的网格点和跨度后,您可以然后可以使用停靠和锚点属性进一步优化布局。

While I find the current up-voted answer quite adequate, it also appears slightly messier than need be. You must add the controls to the tableLayoutPanel before setting their properties.

Visual Studio (2013 and likely other versions) will show these properties as part of the control. When in reality, they are part of the tableLayoutPanel.

Explanation:

tableLayoutPanel.Controls.Add(**control**, x, y)
tableLayoutPanel.SetColumnSpan(**control**, '# of cols to span')

Example:

tableLayoutPanel1.Controls.Add(**button1**, 0, 0);
tableLayoutPanel1.SetColumnSpan(**button1**, 2);
tableLayoutPanel1.SetRowSpan(**button1**, 3);

Result: A button which 'occupies' this space. (Provided it is large enough to cover the area. Even if it does not 'cover' the space, it will still 'reserve' it.)

O O X X X
O O X X X
O O X X X
X X X X X
X X X X X

Setting the span larger than the size of the grid will.. :
NOT change the grid size.
NOT crop/edit the number to the size of the grid.
NOT throw an error at compile.

It WILL act/perform as if the span was set to the current grid (tableLayoutPanel) maximum size. This is only relevant if the TLP/grid size changes.

If you add two controls two the same grid location programmatically, the first control in a grid keeps its location. Any subsequently added control gets pushed to the next cell block. If a 'span' is added, it will treat that cell block as used and continue searching for an unused cell block.

Ex: label1, label2 and label3 are added to 0,0.

  • label1 will appear in 0,0
  • label2: 0,1
  • label3: 0,2

Ex 2: label 1 has a row span of 2.

  • label1: 0,0
  • label2: relocated to 0,2
  • label3: 0,3

After you have selected the correct grid point and spans, you can then further optimize your layout using the dock and anchor properties.

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