change all controls before displaying

Developer

Active member
Joined
May 30, 2007
Messages
29
Programming Experience
Beginner
In my form load event, I change background images of controls. The problem is that people can see one control by one changing images when it loads up .

Also there is a specific button on the page that again changes the background images of all the controls on the page and some controls shift to the side of the page. you can see the changes to the GUI one by one here too.

Is there away around this for at least one of the situations????

A short example would be:

VB.NET:
Private sub form1_load(byval sender as object, e as system.eventargs) handles me.load
for each btn as button in Groupbox1.controls
btn.backgroundImage = my.resources.bluebutton
next
end sub

Private sub Button1_Click(byval sender as object, e as system.eventargs) handles button1.click
for each btn as button in Groupbox1.controls
btn.backgroundImage = my.resources.redbutton
next
groupbox1.location = new point(100,100)
groupbox2.visible = true
end sub
Is there a way to make all buttons change it's colour first before showing???
 
Last edited by a moderator:
Maybe you can hide the buttons first then change whatever you want to change then show it when the changes are done.

I hope this will give you an idea.:D
 
You will certainly find that it does so not only on loading, but also whenever you ask the buttons to repaint. That is hardly something you could solve in a single line of code, however you could consider using labels, pictures boxes or even a custom control (anything with less overhead than a button) to see if it gives better performance.

Use the flat style (you probably already do), try using an imagelist component and try to tweak your images so that they do not have to be stretched or zoomed prior to display.

Also, you may want to use the Image property instead of the backgroundimage property. I have not tested it, but since background image is used to paint under anything else the component displays, it might force the repaint of more stuff than an image which is higher on the z axis. You may also want to try setting TextImageRelation to ImageAboveText unless you actually display some text over your images.


You should also set all the properties you can in the designer as they are otherwise set to they default values in the designer before you set your new value, possibly slowing things down a little.

If none of these do you any good, you may get lucky painting the components manually by handling the Paint event and not using an image (in the case all you need is some regular shape on your buttons).

As a last resort, you could try to manually draw a large component that simulates the shapes and behaviors of many buttons in a single component.

This is pretty much the list of actions I would try in your situation... Someone else might have a better grasp of Windows Forms and help you further, but I can do nothing more...
 
Thanks for the replies.

making the groupbox invisible doesn't really help, when the group box is set to visible again it looks like it paints each button one by one which looks really ugly.

I've tried using the Doublebuffered property but since i'm changing one button by one it doesn't work for this situation. I noticed that making the group box visible = false, setting all the buttons and the making the group box visible again still gives the same effect as changing one button by one, strange yes!!

Yes I noticed when it loads but also when the groupbox is resized or the location changes the same effect happens. I have tried the using the paint event with no luck so far, maybe you could help me out with this (A little bit of source code maybe if you have the time that is)

In the mean time, some more sugestions from all the experts who has been through this phase before would be awesome!!! I'm sort if at a point where I think it isn't possible? Is it?
 
I've tried using the Doublebuffered property but since i'm changing one button by one it doesn't work for this situation. I noticed that making the group box visible = false, setting all the buttons and the making the group box visible again still gives the same effect as changing one button by one, strange yes!!

The bottleneck is not when the button's image is set, but when the button displays (or "paint") that image on the screen. Consider that when you set the properties of the button, you only tell it how it will paint itself, but the actual action only occurs when they appear on the screen. It truly takes the background image and paints it on the screen only then.

What you could do is to use a Label control instead. They have a Click event too, but they produce less overhead (doesn't react to the mouse and stuff), so they should be faster. I tried placing 50 buttons on a Form, then 50 labels and it seemed to reduce the paint time by at least 50% (subjectively). Another option may be the picture box which is made especially for displaying images, or even a custom control you would paint yourself, but I would try that as a last resort only as it is more complicated and if not done properly, may simply be slower than the alternative you were trying to avoid.

If you still want to try this last one, you'll need to handle the paint event and use the e.Graphics reference to draw directly on the screen instead of the backcolor. It all depends on how you use the methods of the Graphics object so that it will be more or less efficient than the other methods. Also, doing this on the button will not prevent the button from painting itself under your implementation, so it will actually take longer to display. You must use a custom control for this.

You should also make sure your images are already the size you display them so that scaling doesn't take any of your CPU cycles (I believe the scaling is also part of the painting part, but it might be in the initialisation part which would make it irrelevant to the problem).

There is no magic line of code that will solve all of this, but there are many ways to make this more efficient and I listed those I could think of in my previous post.

I pointed the way, but I cannot tell the best one for your situation. You'll need to try a few to find the right path.
 
Back
Top