User Controls transparent

G

Guest

I'm using VS 2003, C#.Net 2003.

I built a simple windows form app. with the following attributes:
- has a BackgroundImage set to a jpeg

I built a very simple user control that contains a picture box set to an
icon. The UserControl constructor sets
SetStyle( ControlStyles.SupportTransparentBackColor, true);
this.BackColor = Color.Transparent;
The control does nothing except display the icon.

In the main windows form, on formload event, I declare and instantiate 30
user controls and give them various locations on the main form.

The Problem: When the main form loads, displays or becomes active, there
are transparent (to the desktop) rectangles in locations where the user
controls are to be displayed. The transparent rectangles then fill in with
the mainform background image and the icons in the usercontrol.

I have identified this issue to be linked the BackColor = Color.Transparent.
If I set the BackColor to a color, this issue goes away. Offcourse, I now
have a solid box around my control

Is there a way keep the usercontrol backgroud transparent while eliminating
this annoying transparent rectangle issue?
 
A

abe545

We have been having similar problems in our application when the form
has a number of controls. This behavior is caused by how .NET handles
painting. Each parent will only paint its area not obscured by child
controls. So, the Form won't paint behind the UserControls after they
are added to the Controls collection of the Form. The reason why you
don't see the transparent rectangles if you set their background color
is that their PaintBackground event will fill the area with their
BackColor property. If it is transparent, nothing will be drawn in
that rectangle until the UserControl's Paint event is fired.

We found that it helps, but does not eliminate, to call Refresh() when
the form is shown. Since you are adding the controls during the
FormLoad event, try calling Refresh() before adding the controls. This
will force the form to repaint itself, and won't leave space for the
controls. While the UserControls won't be painted immediately (you
will probably see them rapidly appear on the screen, just not at once),
you at least won't see "holes" in your Form.

Hope this helps.
Abe
 

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