【问题标题】:Automatically resize a form with a TableLayoutPanel that is populated dynamically with controls in its cells使用在其单元格中动态填充控件的 TableLayoutPanel 自动调整表单的大小
【发布时间】:2015-03-11 06:37:42
【问题描述】:

我有一个表格,里面有一个 TableLayoutPanel,它有 1 行和 1 列。我用控件动态地填充这个 TableLayoutPanel。

首先我创建表:

 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));
                    }
                }
            }
        }

然后在其单元格中添加控件:

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

我还创建了这个函数来根据 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;
            }
        }

并在表单设置结束时调用它。

问题是,例如,如果我首先有一个 tableLayout (col=2, row=3),并且 i 传递给表格布局为 (col = 3, row = 1) 维度的情况表单的大小与以前的相同,因此我必须手动调整表单的大小。我希望我的表单根据列数和行数自动调整大小。

有什么想法吗?

谢谢

【问题讨论】:

    标签: c# winforms tablelayout


    【解决方案1】:

    将 TableLayoutPanel 和 Form AutoSize 属性设置为 true。 这意味着 TableLayoutPanel 会根据其内容的大小(添加的控件)自动调整大小,而 Form 会根据其内容(TableLayoutPanel)自动调整大小。

    如果调整大小没有自动完成或看起来很跳跃,您可以使用 SuspendLayout、ResumeLayout 和 PerformLayout 方法来控制调整大小的时间。

    【讨论】:

      【解决方案2】:

      假设我对您的理解正确,请确保 tblLayout 设置了它的自动大小属性,然后将函数 forceSizeLocationRecalc 替换为:

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

      这将强制表单采用表格布局面板的大小。显然,当表格发生变化时,您仍然需要手动调用它。

      您可以在构建表格布局面板的位置添加此内容,以避免必须这样做:

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

      希望有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多