SuspendLayout / ResumeLayout是否毫无价值,还是我错了?

问题描述 投票:2回答:2

我有两个托管TableLayoutPanels的标签页,我用标签和文本框动态填充。第一个获得96个标签和96个文本框,其闪烁是可接受/可容忍的,所以我没有费心添加SuspendLayout / ResumeLayout对。

然而,第二个获得96个标签和288个文本框,其绘画/闪烁是无法容忍的。 IOW,192个控件似乎没问题,但384肯定不行。

我在动态创建控件之前调用SuspendLayout,然后在finally块中调用ResumeLayout,但删除了它们,瞧!像第一个tabPage / TLP一样,闪烁是可以接受的。

为什么减法加法有效呢?

c# winforms dynamic tabs tablelayoutpanel
2个回答
5
投票

您也可以尝试我在此主题中列出的两种方法。希望他们不是太神秘:

https://stackoverflow.com/a/15020157/1307504

这种方法确实暂停并恢复布局。但你永远不应该忘记打电话给EndControlUpdate()

我在我正在创造的任何一般控制中使用它。我尝试了暂停和恢复布局,尝试了很多。它从来没有像我想象的那样工作。


1
投票

最初,我有同样的疑问,SuspendLayoutResumeLayout确实有效。然后我尝试了自己并创建了一个示例应用程序,并在以后更好地了解了这个概念。

所以,这就是我做的:

mainPanel.SuspendLayout()

create child control

call child.SuspendLayout()

change the child control properties

add the child control to the mainPanel

call child.ResumeLayout(false) - this means: next layout run, relayout this control, but not immediately

repeat (2-6) for every child-control

call mainPanel.ResumeLayout(true) - this means: relayout my mainPanel and every child-control now!

另外要证明我的概念这里是示例应用程序

Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        this.SuspendLayout();
        for (int i = 0; i < 2000; i++)
        {
            var textbox = new TextBox();
            //textbox.SuspendLayout();
            //textbox.Dock = i% 2 ==0 ? DockStyle.Left : DockStyle.Right;
            textbox.Dock = DockStyle.Fill;
            textbox.Top = i * 10;
            textbox.Text = i.ToString();
            this.Controls.Add(textbox);
            //textbox.ResumeLayout(false);

        }
        stopWatch.Stop();
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",ts.Hours, ts.Minutes, ts.Seconds,ts.Milliseconds / 10);

        this.ResumeLayout(true);
        MessageBox.Show(elapsedTime);
© www.soinside.com 2019 - 2024. All rights reserved.