Show method of Panel is very slow

I

illegal.prime

Hi all, I'm looking at trying to improve the performance of showing a
bunch of controls on a panel.

I have a class that extends Panel. In that class, I do the following:

this.SuspendLayout();
this.Hide();
//add controls to "this"
AddControls();
this.ResumeLayout();
this.Show();

I have placed timing code in the above and the invocation of the Show
method is extremely slow (relatively speaking to everything else). All
of the other operations (including my custom AddControls method) take
less than half a second. Meanwhile Show always takes anywhere from a
second and a half to two seconds.

I have to use the above ordering in order to ensure the Panel gets
scroll bars to display properly since each invocation to the above code
can result in a completely different set of controls (ranging from 1
control to 50 or more controls). Also, if I don't use Hide first, then
the insertion of the controls can end up taking over 2 seconds.

The only paths I can think of pursuing are either:
a) finding some other way to get the Panel to figure out how much it
needs to scroll (i.e. something like Invalidate), and/or
b) determining why the show takes so long on each of contained controls

Any suggestions on how I could pursue A or B or if there is an option
C? Each of the controls I add are custom controls.

Thanks,
Novice

PS Not that it matters (since .Net is .Net) - but I'm using C#
 
J

Jared

form.doublebuffered =true
controls.addrange()

I have form with 50 dynamic controls and it would be accessed ounce a year
so I just leave it.
 
I

illegal.prime

Yeah, I'm actually using the Controls.AddRange in my method.

It looks like thd DoubleBuffered property is set on the classes that
extend Control, not Form.

Any other suggestions out there to make a large number of controls
"Show" more quickly?

Novice
 
I

illegal.prime

Oddly enough double buffering actually slows the loading of my
controls. I've added the following to their base class constructor and
it actually slows their display (even on subsequent loads of the same
controls). Here is the syntax I'm using to enable double buffering -
I'm not using .Net 2 (and I can't use it):
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);

Anybody else have any suggestions?

Thanks,
Novice
 
G

Geoff Munday

Show() and Hide() just set the this.Visible property to "true" and
"false" respectively. I would suggest removing them.

try this...

this.SuspendLayout();
this.Controls.AddRange();
this.ResumeLayout(false);
this.AutoScroll = true;

Regards,

Geoff
http://nonspect.com
 
G

Geoff Munday

Additionally, are you adding the controls to the Panel before you add
it to its Parent control (Form, UserControl, etc.)? If not try that.
 
G

Geoff Munday

Oh yeah, and forget your SetStyle() calls unless you are actually
performing painting logic of your own. I'm not certain it will help
you in this context.
 
T

tommaso.gastaldi

Hi Novice

I also noticed that .net is an elephant when it comes to loading forms
and controls. Especially forms with several controls take really too
much to load. In general I feel that everything has to do with GUIs is
really too slow. I hope that at Ms will make an effort to improve this
area.

(Not to speak of the general usability of vs2005 GUI which has
dramatically dropped... but this is another matter)

Probably there isn't much one can do more than suspending the layout
and usig addrange. Not sure that hiding the form would help (actually I
had this sensation with VS2003 but no more with VS2005). If you test it
let us know. In you case, it seems that the scrolling is an additional
factor slowing down the loading.

Have you tried setting first the autoscroll minsize, to avoid
scrolling?

In general you could play with the autoscroll position and autoscroll
minsize to see if it helps. (Note that setting the autoscroll is kind
of tricky as the the sign of the x/y positions is opposite wrt to
setting it).

Let us know if this is an area of improvement and your final
conclusions...

-tom

(e-mail address removed) ha scritto:
 
I

illegal.prime

That is actually much worse.

Not really sure why - but I would guess it is because the panel is
showing all its controls and when they are added using the AddRange
method, they end up getting handled individually internally. As
opposed to if the Panel is set to Hide, the individual controls can all
be added without concern about whether they are shown or not - they
when Show is called all the controls are handled at once.

Again, I'm not really sure why your ordering makes it worse, but the
above is my best guess as to why.

Thanks anyway,
Novice
 
G

Geoff Munday

As a test move "this.AutoScroll = true" to before the "SuspendLayout()"
call. This might help because this.AutoScroll forces a Layout event
whenever its value is set.

Are the controls you are adding standard windows controls? or are you
using a lot of custom controls, complex UserControls, and/or thrid
party controls?
 
I

illegal.prime

This isn't really an option for me. In my form I have a class that
extends panel that is added once. As users use the application
different controls are loaded into this panel. Therefore, the ordering
is:
1. Creation of Form (includes addition of object whose class extends
Panel - call it Foo)
2. Various controls are added to Foo
3. User does some things which results in...
4. All controls currenty displayed to be disposed (necessary or else
UserObjects exceeds windows capacity)
5. A different set of controls are added to same Foo object previously
mentioned

Do you think it would be quicker if I did this:
1. Creation of Form
2. Various controls are added to Foo
3. Foo is added to Form
4. User does some things which results in...
5. All controls currenty displayed to be disposed (necessary or else
UserObjects exceeds windows capacity)
6. Foo is removed from Form
7. New instance of Foo is created
8. A different set of controls are added to same Foo object previously
mentioned
9. Foo is added to Form

Novice
 
I

illegal.prime

I tried the AutoScroll to true before SuspendLayout and it did worse
and also the scroll bars didn't appear.

They are all controls that have a base class that extend UserControl.
Most of them just have a bunch of standard windows controls contained
within them though. Though a few of them do get pretty complex with
geometric/graphical stuff.

Thanks for the suggestion though,
Novice
 

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