• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5017
  • Last Modified:

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

Hello,

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 VB.net 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
0
SterlingMcClung
Asked:
SterlingMcClung
  • 4
  • 2
3 Solutions
 
x77Commented:
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.
0
 
SterlingMcClungAuthor Commented:
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.
0
 
x77Commented:
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%).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SterlingMcClungAuthor Commented:
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.
0
 
SterlingMcClungAuthor Commented:
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.
0
 
SterlingMcClungAuthor Commented:
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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now