469,945 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

User Controls transparent

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?

Mar 7 '06 #1
1 2742
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.

Mar 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Benny | last post: by
7 posts views Thread by Chuck | last post: by
1 post views Thread by Efkas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.