TableLayoutPanel滚动条在特定的行数上出错了? [英] TableLayoutPanel scrollbars bugged at specific amount of rows?
问题描述
我使用TableLayouPanel,以编程方式添加每个高度为50px的行. TLP的y尺寸为217像素,因此添加5行会导致垂直滚动条,但也会导致水平滚动条.
I use a TableLayouPanel, programmatically add rows of 50px height each. TLP y-size is 217 px, so adding 5 rows results in a vertical scrollbar but also a horizontal scrollbar.
当我添加另一行时-仍然是垂直滚动条-水平滚动条消失了.
When I add another row - still vertical scrollbar - the horizontal scrollbar disappears.
试图将TLP的大小更改为200到250之间的任何值,但保持不变.
Tried to change the TLP's size to anything between 200 and 250, stays the same.
我如何删除此行为,因为任意数量的行但5行都可以正常工作?
How do I remove this behaviour as any number of rows but 5 works fine?
设计器中的代码并添加行:
Code from designer and adding rows:
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行的水平滚动条,以及如何使内容再次适合外部控件,所以没有?
应该通过添加另一行并再次删除来解决此问题,但是由于TableLayoutPanel
没有给出fck,最后一行会自动占用已删除行的剩余"空间...至少这就是我当前的进度看起来像不确定我是否应该将所有内容都更改为DataGridView
Thought of fixing this by adding another row and deleting it again, but since TableLayoutPanel
doesn't give a fck, the last row automatically takes up the "remaining" space of the removed row...atleast that's what my current progress looks like, not sure if I should change everything to DataGridView
推荐答案
我尝试了很多不同的方法,并找到了适合我的解决方案:
I tried a lot of different things and found a solution working for me:
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);
}
和(如果需要滚动条之前行数少于空格,则空行具有可变大小,以保持最后一行的大小)
and (empty row with variable size to keep the last row's size if there are less rows than space before a scrollbar is necessary)
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
是实际添加的非空行的数量.
For anyone wondering: inp.Count
is the amount of actually added non-empty rows.
也许可以重新设置格式并缩短它,因为它看起来很多余,但是我花了很长时间使它完全可以工作,现在应该还可以.
It might be possible to reformat and shorten this, as it looks redundant, but I worked long enough to get it to work at all, should be fine for now.
这篇关于TableLayoutPanel滚动条在特定的行数上出错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!