dynamically adding controls and slow speed!!

G

Guest

Hello,

I'm developing an application where the user gets the ability to choose how
the form looks like. What is shown, wich controls to use and all available
properties of those controls.
The data gets pulled from a database.

The only problem I have is the speed. When having a form with 500 controls
it takes about 7-8 seconds to show that form to the user. Which is too slow.

At first my guess was that the database was slowing the app down. Or the
looking up of the types and assembly loading for the controls it self.

But then I ran a little experiment. I created a form with one button on it
with the following event-handler.

void button1_click(object sender, EventArgs e)
{
this.SuspendLayout();
for (int i = 0; i < 1000; ++i)
{
Checkbox temp = new Checkbox();
this.Controls.Add(temp);
}
temp.ResumeLayout();
}

I would say this is real basic stuff, but it turns out that this also needs
7 seconds to show the the new controls.

Is there any way I can speed up this kind of operation?

Best regards,
Ike Casteleyn
 
G

GAZ

Your code works just fine. I tested it on my development computer and it is
almost instantenious. Only changed:

temp.ResumeLayout();

into

this.ResumeLayout();

My computer is a Pentium 4 3.2GHz, 1GB RAM.

BR,

GAZ
 
G

Guest

Hi Gaz,

Thanks for the response.

I have retested my app using the following code

private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Stopwatch sp =
System.Diagnostics.Stopwatch.StartNew();
this.Cursor = Cursors.WaitCursor;
this.SuspendLayout();
for (int i = 0; i < 1000; ++i)
{
CheckBox temp = new CheckBox();
this.Controls.Add(temp);
}
this.ResumeLayout();
this.Cursor = Cursors.Default;
sp.Stop();
MessageBox.Show(sp.ElapsedMilliseconds.ToString());
}

I just added the stopwatch stuff to have a clear look on the time to show
the controls.
On pc1 (p4 2.8ghz 1gb ram winxp) -> 2400 milliseconds
On pc2(amdxp 1700 512mb ram win2k) -> 4000 milliseconds.

Could you run this in your pc once again with the adjusted code. Just to
know what 'instantenious' is like timewise. I ran the app with only vs2005
open. Last time I probably had around 7 solutions open (maybe that ate a lot
of mem to go to 7 seconds).

Best regards,
Ike
 
G

GAZ

Hi Ike,

I've run your code and came up with a result of 1807. It seems that the
process itself is rather CPU intensive. I added a counter in the performance
monitor to follow application's percentage of processor's time and the
spikes were impressive. It seems that your application will be directly
dependent on the processor power of the target computer.

BR,

GAZ
 
G

Guest

Hi Gaz,

Thanks for the info. Seems I won't be able to do anything then to improve.

Best regards,
Ike
 
G

GAZ

Hi Ike,

Would your application really need forms with such large number of controls?
From my experience however advanced users are they start to get lost if the
form has more than 30ish controls. It is a large amount of visual
information to process at the same time, especially having in mind that the
users would have to scroll up and down to read parts of the form.

I don't know what kind of application you are building but I think that it
is a safe bet that users won't have more than a 100 controls per a form. It
would not be a good and useful user interface.

Another thing you might consider doing if you get stuck with a large number
of controls is to place them in different tabs. In that way you could
actually add controls to each tab separately once the user click on a
specific tab. That would slow-down a bit tab generation, but it would be
somewhat faster than add all controls at the same time.

BR,

GAZ
 
G

Guest

Hi Gaz,

I know that too much controls are bad for user experience. We also develop
our static forms using tabs and such to take the feeling of a screen full of
controls (also to get data loading down).

Problem for us is that these forms are dynamic. The user decides what's on
the forms (the app pulls the info from a database) and we allready have some
with a form of 500 controls.
However I didn't give the possibility to have a tabcontrol to work with.
A solution might be found in that direction. Thanks for the tip.

Best regards,
Ike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top