简体   繁体   English

C#TableLayoutPanel填充

[英]C# TableLayoutPanel fill

In the TableLayoutPanel, by default, the last element in a row or column takes up the remaining space. 默认情况下,在TableLayoutPanel中,行或列中的最后一个元素占用剩余空间。 How can I achieve a layout where the penultimate or any other element takes up as much space as possible, and among others, the last element takes up only as much space as is necessary? 如何获得倒数第二个元素或任何其他元素占用尽可能多的空间,而最后一个元素仅占用所需空间的布局?

It depends on how the other rows are set. 这取决于其他行的设置。 Assuming all other rows are auto-sized or of fixed size, setting the penultimate row's size to 100% does what you want. 假设所有其他行都是自动调整大小或固定大小,那么将倒数第二行的大小设置为100%就可以了。 The designers generated code looks like this: 设计人员生成的代码如下所示:

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());

I've tested this using Windows Forms and .Net Framework 4; 我已经使用Windows Forms和.Net Framework 4进行了测试;

The simplest solution seems to be to create a table layout panel, and embed your controls in each of the columns. 最简单的解决方案似乎是创建一个表布局面板,然后将控件嵌入每个列中。 Once done, set the size of all of the columns to "AutoSize". 完成后,将所有列的大小设置为“ AutoSize”。

Then take your penultimate column and set the size to be "100%" 然后,选择倒数第二列并将大小设置为“ 100%”

Here's the code from my designer: 这是我设计师的代码:

    tableLayoutPanel1 = new TableLayoutPanel();
    tableLayoutPanel1.ColumnCount = 4;
    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
    tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());

Be sure to embed your controls first, as, if there is no content in the other cells, setting the penultimate column to size 100% then makes it tricky to use the designer to put controls in the empty cells as the 100% column effectively "steals" their space. 确保先嵌入控件,因为如果其他单元格中没有内容,则将倒数第二列设置为100%的大小,然后很难使用设计器将控件作为100%列有效地放置在空单元格中。窃取”他们的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM