简体   繁体   English

具有嵌套自动大小调整的用户的TableLayoutPanel控件性能问题

[英]TableLayoutPanel with nested autosized user controls performance issue

I'm facing with nasty performance issue when using TableLayoutPanel. 使用TableLayoutPanel时,我面临令人讨厌的性能问题。 I have a simple user control with RadioButton and LinkLabel. 我有一个带有RadioButton和LinkLabel的简单用户控件。 Text of LinkLabel is dynamic so entire control have AutoSize property set to true. LinkLabel的文本是动态的,因此整个控件的AutoSize属性都设置为true。

Now I have a Panel with AutoScroll set to true and TableLayoutPanel auto sized and with 2 columns inside it. 现在,我有一个面板,其中AutoScroll设置为true,TableLayoutPanel的大小自动调整,并且其中有2列。 This TableLayoutPanel is populated with above-mentioned user controls: 该TableLayoutPanel装有上述用户控件:

private void PopulateLocationItemsTable(Control[] Controls)
{
    //Suspend outher Panel and set AutoScroll to false just in case.
    panelLocationItemsTableCountainer.SuspendLayout();
    panelLocationItemsTableCountainer.AutoScroll = false;
    //Suspend TableLayoutPanel
    tableLocationItems.SuspendLayout();
    Controls = Controls.OrderBy(c => c.Text).ToArray();
    //Populate left column
    int verticalPosition = 3;
    int leftColumnControlsNumber = Controls.Length / 2;
    for (int i = 0; i < leftColumnControlsNumber; i++)
    {
       tableLocationItems.Controls.Add(Controls[i], 0,0);
       Controls[i].Location = new Point(10, verticalPosition);
       verticalPosition += 17;
    }
    //Populate right column
    verticalPosition = 3;
    for (int i = leftColumnControlsNumber; i < Controls.Length; i++)
    {
        tableLocationItems.Controls.Add(Controls[i], 0, 1);
        Controls[i].Location = new Point(10, verticalPosition);
        verticalPosition += 17;
    }
    //Resume TableLayoutPanel
    tableLocationItems.ResumeLayout(true);
    //And restore outher Panel state
    panelLocationItemsTableCountainer.AutoScroll = true;
    panelLocationItemsTableCountainer.ResumeLayout(true);
}

The problem is that user controls initially populated in FormLoad event and the Form just hangs for around 10 seconds before it actually appears. 问题在于,最初在FormLoad事件中填充了用户控件,而该Form在实际显示之前仅挂起了大约10秒钟。 This is completely unacceptable for me. 这对我来说是完全不能接受的。

This issue goes away if I set AutoSize property of user control to false. 如果将用户控件的AutoSize属性设置为false,则此问题消失。 I also was tried to put user controls directly to the outher Panel and it also works fine. 我还尝试过将用户控件直接放置到外部面板上,并且也可以正常工作。 The problem is just with TableLayoutPanel. 问题仅在于TableLayoutPanel。 Is anyone faced such issue and found the solution? 是否有人遇到这样的问题并找到了解决方案? Of corse I can place my user controls myself directly to the Panel calculating right coordinales but using TableLayoutPanel is a "correct" way for such tasks. 当然,我可以将用户控件直接放置在面板上,以计算正确的坐标,但是使用TableLayoutPanel是执行此类任务的“正确”方法。

Using the TableLayoutPanel is the right approach but you'll want to think of the columns in that control as static widths. 使用TableLayoutPanel是正确的方法,但是您需要将该控件中的列视为静态宽度。 I had an application where I faced almost exactly the same problem using that panel and realized I'd just been looking at it all wrong. 我在一个应用程序中使用该面板遇到了几乎完全相同的问题,并且意识到我只是在错误地看待它。

If there are two columns, and my container (eg a form) is 300 pixels wide, then each column is 150 pixels wide (minus padding and stuff) so the controls inside those columns have to react rather than the columns reacting. 如果有两列,而我的容器(例如表单)的宽度为300像素,则每列的宽度为150像素(减去填充和填充),因此这些列中的控件必须起作用而不是列起作用。

The other reason you really need to look at it this way is because the engine doesn't layout everything in memory first (like the WPF framework does) so it's extremely in efficient at its core since it commits the changes immediately. 您真正需要以这种方式查看它的另一个原因是,因为引擎没有像在WPF框架中那样首先在内存中布局所有内容,因此它的核心效率极高,因为它可以立即提交更改。

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

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