Adding controls to a usercontrol at runtime is slower than adding controls to a winform

Posted on 2011-10-31
Last Modified: 2012-05-12

I have created a new UserControl, that will be used for product and feature selection.  It has a combobox and a numericUpDown control and as the users select a product from the combobox new instances of the usercontrol are nested into the control at runtime, to allow the user to select the features that are available for the selected product.  I had originally created most of this functionality without the usercontrol, but found it very difficult to track the events properly.  The problem right now, is that when I add the nested usercontrol the screen updates very slowly.  If I add more of these UserControls to my form, it is very fast and if I use my logic to add the comboboxes and numericUpDown controls directly in the form, it is fast.  Also removing the nested UserControls happens very fast as well.  I have been unable to determine why the addition of the nested controls is taking so long.  I have made sure that I am calling SuspendLayout on my panels before adding the new controls at runtime, but this does not seem to have any effect.

I am working on cleaning up some of the code to make create a test project that is easier to post, in the meantime, are there any general ideas of what might be causing this to be slow?  I maybe should be specific about what I feel is slow.  When I add controls, including a single instance of the UserControl, directly to the form, it seems to complete in under 1/4 second; barely noticable when working with the form.  However, adding a single nested UserControl takes much closer to 1/2 second, which is noticable, but possibly acceptable.  The problem is that it will be common to have to add 3-4 of these nested controls at a time and the time require seems to be linear; adding 3 controls takes over a second to complete.  I don't think that this will be an acceptable delay for the application.

I am programming in on framework 3.5 using VS 2008 Professional, and answers in C# are acceptable, as I should be able to translate to VB.

Thanx in advance,
Sterling McClung
Question by:SterlingMcClung
    LVL 15

    Assisted Solution

    1 - Use addrange:

        Myform.Controls.addrange( new Control(){control1, control2, control3})

    2 - Use SuspendLayout  - ResumeLayout

    If you inspect Formxxx.Designer.Vb , you can note that it does also.
    LVL 7

    Author Comment

    The way my logic runs right now, I can't add all the controls with AddRange.  I can look at reworking this, if needed.  I had thought that AddRange would be needed for larger a number of controls, though.  Should it really be making this much of a difference?

    I am using SuspendLayout and ResumeLayout around the logic where I add the control.  Right now I am suspending before I even instantiate the new control and resuming at the end of my processing.  I could tighten the SuspendLayout - ResumeLayout calls to right before/after adding the controls to the panel.

    Do you have ane explanation for the fast remove, but slow add?  To me, thet should require similar amounts of work.
    LVL 15

    Accepted Solution

    There are some reasons that can slow down the Controls.Add to Form.

     Have you AutoScroll ?, When  you use AutoScroll, it need evaluate all logic for all controll to verify autoscroll dimensions.

    You can try to allocate controls on a fixed panel (without autoscroll), then scroll the panel on your form.

    Some time ago, I make a Filter menu with submenus based on data. I create It dynamic, this is very Slow without Addrange (I add some times until 30 options on each menu). With addrange, I don´t apreciate any delay.

    The efect of SuspendLayout is apreciable only when yuo measure performance - StopWatch (about 20 / 30%).
    LVL 7

    Author Comment

    For the UserControl, I place a Combobox, NumericUpDown and a MaskedTextBox inside a Panel, which I then place in a FlowLayoutPanel in TopDown FlowDirection and AutoSize on with GrowAndShrink.  When I nest the control, I place the new instance inside the FlowLayoutPanel of the "Parent" UserControl.  On the main Form, the UserControls are added to another FlowLayoutPanel, again with TopDown FlowDirection and AutoSize on with GrowAndShrink.  Each control that gets added to the main Form's FlowLayoutPanel represents a Product with all the features that the user has selected(each feature coming from one of the nested UserControls).  The main form may have only 5 products, represented by 5 UserControls, although each of the 5 UserControls may include 1-6 different UserControl instances within the single UserControl that is added to the main Form.

    AutoScroll is turned off on all of my different panels, but it is turn on for the main form.  I just tested with it turned off for the main Form as well, and there is now change.  I do suspect that the AutoSize properties of the panels are having a similar effect on the performance, though.

    I have started to do some preliminary performance measuring with StopWatch, and I will try to track things down a little more.  I will also see if I can get things to go faster without my current autosize settings and try to use AddRange as much as possible.  If those still don't work, then I will put together a test program to work from.
    LVL 7

    Assisted Solution

    Thanx for all the feed back.  It turns out that the delays in getting the controls added to the form had nothing to do with the control.add() function.  As I worked more with stopwatch to find the bottlenecks, it turned out that when I was populating the combobox's datasource, I was hitting the database.  I thought that the tools I was using for DB access would keep the data that I was requesting in memory from an earlier request,  However, this was not the case, so if I added 3 usercontrols, I was hitting the DB 3 times.  So the delay that I was seeing was due to the DB roundtrips and not the actual call to controls.add().  While I feel there is still some room for improvement, I am quite satisfied with where things are now.
    LVL 7

    Author Closing Comment

    While I ultimately found the solution myself, the answers provided appear to be correct, if my original question would have actually had been my original problem.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now