TableLayoutPanel 滚动条在特定行数处出现错误?

TableLayoutPanel scrollbars bugged at specific amount of rows?

我使用 TableLayouPanel,以编程方式添加每行 50 像素的高度。 TLP y 大小为 217 像素,因此 添加 5 行 会产生垂直滚动条和 水平滚动条

当我添加另一行 - 仍然是垂直滚动条 - 水平滚动条消失

尝试将 TLP 的大小更改为 200 到 250 之间的任何值,但保持不变。

如何删除此行为,因为任何行数但 5 行都可以正常工作?

设计器代码和添加行:

System.Windows.Forms.TableLayoutPanel tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.ColumnCount = 3;
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel1.Size = new System.Drawing.Size(709, 217);


tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}
for(int i = 0; i < 5; i++) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
    tableLayoutPanel1.Controls.Add(new Control(), 0, i);
    tableLayoutPanel1.Controls.Add(new Control(), 1, i);
    tableLayoutPanel1.Controls.Add(new Control(), 2, i);
}
if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

tl;dr 为什么有一个 5 行的水平滚动条以及如何使内容再次适合外部控件所以有 none?


想通过添加另一行并再次删除来解决此问题,但由于 TableLayoutPanel 没有给出 fck,最后一行自动占用 "remaining" space删除的行...至少这就是我当前的进度,不确定我是否应该将所有内容更改为 DataGridView

我尝试了很多不同的方法并找到了适合我的解决方案:

tableLayoutPanel1.RowStyles.RemoveAt(0);
for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; i--) {
    tableLayoutPanel1.Controls[i].Dispose();
}

变成了

int i;  
while ((i = tableLayoutPanel1.Controls.Count) >= 2) {
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();      
    tableLayoutPanel1.Controls[--i].Dispose();  
}   
while (tableLayoutPanel1.RowStyles.Count > 0) {         
    tableLayoutPanel1.RowStyles.RemoveAt(tableLayoutPanel1.RowStyles.Count - 1);
}

和(如果行数少于 space,则在需要滚动条之前,可变大小的空行保持最后一行的大小)

if (!tableLayoutPanel1.VerticalScroll.Visible){ //false for 5
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
}

变成了

tableLayoutPanel1.Update(); //不确定如果需要

if (!tableLayoutPanel1.VerticalScroll.Visible) {
    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
    tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
    tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
} else if (tableLayoutPanel1.HorizontalScroll.Visible) {
    if (tableLayoutPanel1.RowStyles.Count != inp.Count) {
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    } else {
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        tableLayoutPanel1.Controls.Add(new Control(), 0, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 1, inp.Count);
        tableLayoutPanel1.Controls.Add(new Control(), 2, inp.Count);
        tableLayoutPanel1.GetControlFromPosition(0, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(1, inp.Count).Dispose();
        tableLayoutPanel1.GetControlFromPosition(2, inp.Count).Dispose();
        tableLayoutPanel1.RowStyles.RemoveAt(inp.Count);
    }
}

对于任何想知道的人:inp.Count是实际添加的非空行的数量。

可能可以重新格式化和缩短它,因为它看起来多余,但我工作了足够长的时间让它可以正常工作,现在应该没问题。