简体   繁体   English

使用TableLayoutPanel自动调整窗体的大小,该表动态填充了其单元格中的控件

[英]Automatically resize a form with a TableLayoutPanel that is populated dynamically with controls in its cells

I've a form with a TableLayoutPanel inside it, it have 1 row and 1 column. 我有一个表格,里面有一个TableLayoutPanel,它有1行和1列。 I populate this TableLayoutPanel dinamically with controls. 我用控件以动态方式填充此TableLayoutPanel。

First i create the table with: 首先,我用以下方法创建表:

 private void generateTable(int columnCount, int rowCount)
        {
            //Clear out the existing controls, we are generating a new table layout
            tblLayout.Controls.Clear();

            //Clear out the existing row and column styles
            tblLayout.ColumnStyles.Clear();
            tblLayout.RowStyles.Clear();

            //Now we will generate the table, setting up the row and column counts first
            tblLayout.ColumnCount = columnCount;
            tblLayout.RowCount = rowCount;

            for (int x = 0; x < columnCount; x++)
            {
                //First add a column
                tblLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

                for (int y = 0; y < rowCount; y++)
                {
                    //Next, add a row.  Only do this when once, when creating the first column
                    if (x == 0)
                    {
                        tblLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    }
                }
            }
        }

then add controls in its cells: 然后在其单元格中添加控件:

tblLayout.Controls.Add(my_control, column, row);

i've also create this function to resize the form on the basis of the number of row and column of the TableLayoutPanel. 我还创建了此函数,以根据TableLayoutPanel的行数和列数来调整窗体的大小。

private void forceSizeLocationRecalc()
        {

            this.Height = 0;
            this.Width = 0;

            int col = this.tblLayout.ColumnCount;
            int row = this.tblLayout.RowCount;

            for (int i = 0; i < row; i++)
            {
                this.Height += this.tblLayout.GetControlFromPosition(0, i).Height;
            }

            for (int i = 0; i < col; i++)
            {
                this.Width += this.tblLayout.GetControlFromPosition(i, 0).Width;
            }
        }

and call it at the end of the setup of the form. 并在表单设置结束时调用它。

The problem is, if for example, i've first a tableLayout (col=2, row=3), and the i pass to a case in which table layout is (col = 3, row = 1) the dimension of the form is the same of the previous, so i've to resize the form manually. 问题是,例如,如果我首先有一个tableLayout(col = 2,row = 3),并且我将表布局设置为(col = 3,row = 1)表格尺寸的情况与以前的相同,因此我必须手动调整表单的大小。 I would like that my form resizing automatically on the basis of columns number and rows number. 我希望我的表格根据列数和行数自动调整大小。

Any idea? 任何想法?

thanks 谢谢

Assuming I've understood you correctly, ensure tblLayout has it's auto size properties set, and then replace the function forceSizeLocationRecalc with this: 假设我已经正确理解了您的信息,请确保tblLayout设置了它的自动大小属性,然后将函数forceSizeLocationRecalc替换为:

private void forceSizeLocationRecalc()
{
     this.Width = this.tblLayout.Width;
     this.Height = this.tblLayout.Height;
}

This will then force the form to take on the size of the table layout panel. 然后,这将迫使表单采用表格布局面板的大小。 Obviously you'll still need to call this manually when the table is changed. 显然,更改表后,您仍然需要手动调用此方法。

You could add this where you construct the table layout panel to prevent having to do that: 您可以在构造表布局面板的位置添加此代码,以防止这样做:

this.tblLayout.SizeChanged += delegate { this.forceSizeLocationRecalc(); };

Hope that helps! 希望有帮助!

Set the TableLayoutPanel and Form AutoSize properties to true. 将TableLayoutPanel和Form AutoSize属性设置为true。 This means the TableLayoutPanel is auto resized based on the size of it's contents (the controls being added) and the Form is auto resized based on it's contents (the TableLayoutPanel). 这意味着TableLayoutPanel会根据其内容(添加的控件)的大小自动调整大小,而Form会根据其内容(TableLayoutPanel)自动调整大小。

If the resizing does not get done automatically or looks jumpy you can use the SuspendLayout, ResumeLayout and PerformLayout methods to control when the resizing occurs. 如果调整大小没有自动完成或看起来有些跳动,则可以使用SuspendLayout,ResumeLayout和PerformLayout方法来控制何时进行调整大小。

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

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