禁用 TableLayoutPanel 行自动缩放

Disable TableLayoutPanel rows autoscaling

我想禁用 TableLayoutPanel 的行自动缩放,以便它适合,例如,宽度为 4 列,高度为 3 行,并且自动滚动也可以工作。我应该改变什么?

代码:

    public UserControl()
    {
        InitializeComponent();
        tableLayoutPanel1.ColumnStyles.Clear();
        tableLayoutPanel1.RowStyles.Clear(); 
        foreach (Picture picture in Program.gallery)
            addImage(picture);
        for (int i=0;i<tableLayoutPanel1.ColumnCount;i++)
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f/4));
        for (int i = 0; i < 99999; i++)
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100f/3));
    }

TableLayoutPanel 有:

AutoScroll=true;
AutoSize=false;
ColumnCount=4;
RowCount=3;
Dock=true;
GrowStyle=AddRows;

百分比大小类型已损坏。 TableLayoutPanel 的滚动条也有问题,没有收缩。 Here

我将这些属性用于 table:

AutoScroll=false;
AutoSize=true;
ColumnCount=4;
RowCount=0;
Dock=Top;
GrowStyle=AddRows;

和外部控制属性:

AutoScroll=true;

检查行数和禁用未使用的代码:

private int cellsCount=0;
private const int rows = 3;
private int CellsCount {
        get => cellsCount;
        set { 
            cellsCount = value;
            int expectedRows = (cellsCount - 1 + tableLayoutPanel1.ColumnCount) / tableLayoutPanel1.ColumnCount;
            while (expectedRows > tableLayoutPanel1.RowCount)
            {
                tableLayoutPanel1.RowCount++;
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, Height/rows));
            }
            while (expectedRows < tableLayoutPanel1.RowCount) {
                tableLayoutPanel1.RowCount--;
                tableLayoutPanel1.RowStyles.RemoveAt(0);
            }
        }
}

并调整侦听器的大小以设置行的真实尺寸:

private void tableLayoutPanel1_Resize(object sender, EventArgs e)
{
    foreach (RowStyle row in tableLayoutPanel1.RowStyles)
        row.Height = Height / rows;
}