Is SuspendLayout/ResumeLayout worthless or am I going about it wrong?

10,456

Solution 1

You may also try the two methods, I have listed in this thread. Hope they are not too arcane:

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

This methods really suspends and resumes the layout. But you should never forget to call EndControlUpdate().

I use this in any generically control I am creating. I tried a lot with alling suspend and resume layout. It never worked the way I thought it should.

Solution 2

Initially, I had same doubt that does the SuspendLayout and ResumeLayout really works. Then I tried myself and created a sample application and got to know the concept much better later on.

So, here is what I did:

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!

Also to Prove My Concept Here is the Sample Application

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);
Share:
10,456
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin &amp; The Eco Defenders Kindle eBook; Taterskin &amp; The Eco Defenders Paperback; Taterskin &amp; The Eco Defenders Hardcover Taterskin &amp; The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on June 05, 2022

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 2 years

    I have two tab pages hosting TableLayoutPanels that I dynamically populate with labels and textboxes. The first one gets 96 labels and 96 textboxes, and its flicker is acceptable/tolerable, so I didn't bother to add a SuspendLayout/ResumeLayout pair.

    However, the second one gets 96 labels and 288 textboxes, and its painting/flickering is intolerable. IOW, 192 controls seems to be okay, but 384 is decidedly not.

    I was calling SuspendLayout prior to creating the controls dynamically, and then ResumeLayout in the finally block, but removed them, and voila! Like the first tabPage/TLP, the flicker is acceptable.

    Why does this addition by subtraction work?

  • Sandip
    Sandip over 6 years
    Don't Forget to Comment out this.SuspendLayout() and this.ResumeLayout(True)