我创建了一个表单并添加了一个TableLayoutPanel(两者都在代码中生成)。面板有2000行,面板有一个滚动条。当我运行应用程序时,它会在显示表单时冻结。当我只添加1700行时,它会显示表单,当快速向下滚动时,程序会在刷新表单内容之前冻结几秒钟。将行添加到Panel时,我使用SuspendLayout和ResumeLayout。我究竟做错了什么?部分代码:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace xxx
{
partial class xxx
{
public int showForm()
{
Form unusedTableForm = new Form();
unusedTableForm.Text = "Unused IOs";
unusedTableForm.AutoScroll = true;
unusedTableForm.AutoSize = true;
unusedTableForm.MaximizeBox = false;
unusedTableForm.MinimizeBox = false;
unusedTableForm.FormBorderStyle = FormBorderStyle.Fixed3D;
unusedTableForm.StartPosition = FormStartPosition.Manual;
unusedTableForm.Location = new Point(20, 20);
unusedTableForm.TopMost = true;
unusedTableForm.VerticalScroll.SmallChange = 300;
TableLayoutPanel unusedTable = new TableLayoutPanel();
unusedTable.RowCount = 1; // +1 for header
unusedTable.ColumnCount = 3;
unusedTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
unusedTable.AutoSize = true;
unusedTable.RowCount = 2000;
for (i = 0; i < unusedTable.ColumnCount; i++)
{
unusedTable.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
}
for (i = 0; i < unusedTable.RowCount; i++)
{
unusedTable.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
// create labels for each cell here
unusedTable.SuspendLayout(); // to make it fast
// add labels into cells here
unusedTable.ResumeLayout();
unusedTableForm.Controls.Add(unusedTable);
if (unusedTableForm.Height > 900)
{
unusedTableForm.Width = unusedTable.Width + 50;
unusedTableForm.Height = 920;
unusedTableForm.AutoSize = false;
unusedTable.Height = 900;
}
unusedTableForm.ShowDialog(); // it freezes here with timeout
}
}
}