[英]TableLayoutPanel extremely slow
I am using TableLayoutPanel
in C# (Forms).我在 C#(表单)中使用
TableLayoutPanel
。 My table is pretty big with its 33 columns and 8 rows.我的表很大,有 33 列和 8 行。 All cells contain
Label
-objects.所有单元格都包含
Label
对象。
I have already set DoubleBuffered = true;
我已经设置了
DoubleBuffered = true;
of my TableLayoutPanel by creating a new subclass:通过创建一个新的子类来更改我的 TableLayoutPanel:
public class DoubleBufferedTableLayoutPanel : TableLayoutPanel
{
public DoubleBufferedTableLayoutPanel()
{
DoubleBuffered = true;
}
}
If a user presses button X, all cell-controls are deleted and other labels are loaded into the table (from an array which contains all Label objects).如果用户按下按钮 X,将删除所有单元格控件,并将其他标签加载到表格中(来自包含所有 Label 对象的数组)。
DEL: this.table.Controls.Remove(this.table.GetControlFromPosition(col, row));
DEL:
this.table.Controls.Remove(this.table.GetControlFromPosition(col, row));
ADD: this.table.Controls.Add(this.labelArray[row, (col+pos)], col, row);
添加:
this.table.Controls.Add(this.labelArray[row, (col+pos)], col, row);
Everything works fine, except that the progress of deleting the controls and adding the new ones takes five to ten seconds.一切正常,只是删除控件和添加新控件的过程需要五到十秒钟。
Is there a way other than to set DoubleBuffered = true
in order to speed up this process?除了设置
DoubleBuffered = true
之外,还有其他方法可以加快此过程吗?
Use this code to avoid the slow processing of events in C#使用此代码可避免 C# 中事件处理缓慢
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.SuspendLayout();
// Processing Code
tableLayoutPanel1.ResumeLayout();
tableLayoutPanel1.Visible = true;
我可能错了,但这听起来像是针对这些任务优化的 datagridview 的工作。
Are you getting the Data from the database to populate the tablelayoutpanel everytime you perform an action (postback)?每次执行操作(回发)时,您是否都从数据库获取数据以填充 tablelayoutpanel?
If so, consider lazy loading the data in to a bindable object once when the page first loads.如果是这样,请考虑在页面首次加载时将数据延迟加载到可绑定对象中。 Then use the populated object when re binding (on Postback).
然后在重新绑定时使用填充的对象(在回发时)。
Try this code试试这个代码
TableLayoutPanel.GetType().GetProperty("DoubleBuffered",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(TableLayoutPanel, true, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.