简体   繁体   English

如何修复在TableLayoutPanel上显示的滚动条?

[英]How to fix scrollbar showing on TableLayoutPanel?

Got a TableLayoutPanel in my winform, to increase loading speed. 在我的winform中有一个TableLayoutPanel ,以提高加载速度。 I tried suspending it's layout, adding the rows and resume Layout. 我试着暂停它的布局,添加行并恢复布局。 Sadly if I suspend until all rows are added, I get a horizontal scroll bar that does not appear if I resume Layout inbefore adding or suspend after adding each row again. 可悲的是,如果我暂停直到添加完所有行,我将获得一个水平滚动条,如果在添加之前恢复布局或在再次添加每行之后暂停,则不会出现水平滚动条。 滚动条

Msdn states "Add method reapplies the table layout to all the controls" : Msdn指出“添加方法将表布局重新应用于所有控件”

That's what I have to reproduce by my own code after all rows are added. 添加所有行后,我必须使用自己的代码来复制。 However adding another row in the end does not produce the layout not suspending at all does. 但是,最后添加另一行不会产生完全不暂停的布局。

I tried: 我试过了:

  • replace the first column's content with new Control() just to test if the content was too large. new Control()替换第一列的内容只是为了测试内容是否太大。
  • change the contents size -> led to smaller content, more whitespace and same problem 更改内容大小->导致内容更小,更多的空白和相同的问题
  • change the tableLayoutPanel's size -> led to a smaller control with still the same problem 更改tableLayoutPanel的大小->导致具有相同问题的较小控件
  • change the first column's size (as the later two ones have absolute values) from 100% to a lower percent value, autosize and a similar absolute value -> led to the last column begin oversized, still visible scrollbar 将第一列的大小(因为后两个具有绝对值)从100%更改为较低的百分比值,自动调整大小和一个类似的绝对值->导致最后一列开始过大,滚动条仍可见
  • PerformLayout() after rows are added 添加行后的PerformLayout()
  • Refresh() after rows are added 添加行后Refresh()
  • ResumeLayout(true) to force pending layout requests/changes (Should absolutely give me the layout I want and get without suspending, maybe buggy or something is overwritten?) ResumeLayout(true)强制执行待处理的布局请求/更改(应该绝对给我想要的布局,而不会暂停,可能有错误或某些东西被覆盖了吗?)
  • Update() after rows are added 添加行后的Update()

tl;dr add rows, lag, no scrollbar vs suspend, add rows, horizontal scrollbar tl; dr添加行,滞后,无滚动条 vs 暂停,添加行,水平滚动条

why and how not to? 为什么以及如何不?


Found out that without all the suspending and resuming I experience the same problem if I add EXACTLY 5 rows: 4 work fine as no scrollbar is needed, 6 remove the horizontal scrollbar, 5 is buggy. 发现如果没有全部挂起和恢复,如果我恰好添加了5行,我会遇到相同的问题:4可以正常工作,因为不需要滚动条,6删除了水平滚动条,5是越野车。 So the problem goes deeper than layout suspending. 因此,问题比布局暂停更深。 There is definitely something wrong with adding a specifing amount of rows overall. 总体上添加指定数量的行肯定有问题。 -> outsourced to new question (required for this one) -> solved outsourced question , ->外包给新问题 (对此是必需的) ->解决了外包问题

paused until I stumble upon problems with Suspending again 停下来,直到我再次遇到暂停问题

This question is linked with a problem I outsourced and solved here , consider the code over there as the main solution, this is a small addition. 这个问题与我在这里外包并解决的问题有关, 这里以代码为主要解决方案,这是一个很小的补充。

As described in the question's comments, there is a specific amount of rows I've to add without suspending (related to the height of the TableLayoutPanel ), this seems to work in most cases ( inp.Count is the amount of rows added) : 如问题注释中所述,我要添加一定数量的行而不暂停(与TableLayoutPanel的高度有关),这似乎在大多数情况下都inp.Countinp.Count是添加的行数)

tableLayoutPanel1.SuspendLayout();
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);
}
tableLayoutPanel1.RowCount = 0;
for(int i=0; i<inp.Count; i++){
    if (i > inp.Count - 6) {
        tableLayoutPanel1.ResumeLayout();
    }
//...
}

Check the above linked solution for that one case where inp.Count is not big enough to fix the horizontal scrollbar. 请检查上述链接的解决方案,以了解inp.Count不足以固定水平滚动条的情况。


This improves the loading time massively, however there is still lag of visual elements, why I decided to add in tableLayoutPanel1.Hide(); 这极大地缩短了加载时间,但是视觉元素仍然滞后,为什么我决定添加tableLayoutPanel1.Hide(); at the beginning and tableLayoutPanel1.Show(); 在开头和tableLayoutPanel1.Show(); at the end. 在末尾。

Even hiding the parent control doesn't prevent the user from seeing how the rows disappear, also after revealing the control again, the table borders are shown first before the content itself is loaded. 即使隐藏父控件也不会阻止用户看到行如何消失,在再次显示控件之后,在加载内容本身之前会先显示表格边框。 So this solution isn't perfect, but the extent it works to shall be enough here. 因此,该解决方案不是完美的,但在这里它起作用的程度就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM