以编程方式更改行/列跨度(tablelayoutpanel)
我有一个表格布局面板。 2x2 - 2 列 2 行。
例如,我在 1 行、第二列中添加了一个按钮 button1。 button1 的停靠属性设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码怎么样?
What about this code?
虽然我发现当前投票的答案相当充分,但它也显得比需要的稍微混乱一些。在设置控件的属性之前,必须将控件添加到 tableLayoutPanel。
Visual Studio(2013 和可能的其他版本)会将这些属性显示为控件的一部分。实际上,它们是 tableLayoutPanel 的一部分。
说明:
示例:
结果:“占据”此空间的按钮。 (只要它足够大以覆盖该区域。即使它没有“覆盖”该空间,它仍然会“保留”它。)
将跨度设置为大于网格的大小将..:
不要改变网格大小。
不要将数字裁剪/编辑为网格的大小。
编译时不抛出错误。
它的行为/执行就像将跨度设置为当前网格(tableLayoutPanel)最大尺寸一样。这仅在 TLP/网格大小发生变化时才相关。
如果以编程方式将两个控件添加到同一网格位置,则网格中的第一个控件将保留其位置。任何随后添加的控件都会被推送到下一个单元格块。如果添加“跨度”,它将将该单元块视为已使用,并继续搜索未使用的单元块。
例如:label1、label2 和 label3 添加到 0,0。
Ex 2: 标签 1 的行跨度为 2。
选择正确的网格点和跨度后,您可以然后可以使用停靠和锚点属性进一步优化布局。
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:
Example:
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.)
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.
Ex 2: label 1 has a row span of 2.
After you have selected the correct grid point and spans, you can then further optimize your layout using the dock and anchor properties.