Labels transparent only when directly on the form (Bug?)

P

Paul

Hey all

I'm aware that labels in VB.Net do not have a backcolour property because
they are supposed to be transparent. Transparency is not problem for me -
I want it to be trasparent. Problem is though, it seems that labels are
only transparent when they are placed directly on the form.
If you place them in a container like a panel (setting the panels backcolour
to the same as the form), in the IDE they are trasparent, but if you run the
app they appear white (or do in my app at least). Is this an over-sight on
my part, or have others noticed the same thing?

Ta.


Paul
 
S

Shanti

My experience is not the same as yours. I have no problem in getting the
panel backcolor as that of the label. I have set panel.backcolor = Magenta
and my label is also shown with Magenta back color.
 
P

Paul

Am currently working on stuff, so don't ave the time to properly test it,
but at first glance it would seem that the Paint Event doesn't fire if the
control (labels and possibly others) is inside another control, such as a
panel.
Again, not tested just a something I noticed.
 
I

Iassen Assenov

Paul said:
Am currently working on stuff, so don't ave the time to properly test it,
but at first glance it would seem that the Paint Event doesn't fire if the
control (labels and possibly others) is inside another control, such as a
panel.
Again, not tested just a something I noticed.


Hi Paul,

in fact labels in .NET do have the BackColor property and according to
the MS documentation you can set it to Color.Transparent value.
According to the same documentation the transparent color value will
only take effect if the 'Style' of the label control has the
ControlStyles.SupportsTransparentBackColor bit set to true. (Note:
This style bit is set to 'true' by default and there is no direct way
to reset it). So far so good. The problem is that the transparent
effect achieved this way is not 'real' transparency. The label is
simply asking the parent to draw the label's background inside the
label's OnPaintBackground method. As you already noticed if you place
a label on top of a TextBox you wouldn't be able to see the TextBox,
you will be actually seeing the background of the parent form. The
even bigger problem is that this behavior continues even if you add a
label dynamically to the layout of another container(panel,
RichTextBox, etc.)
The only way to achieve true transparency in a label control is to
create your own version by inheriting from the standard label control.

After spending couple of hours reading through documentation and other
people experiences (thank you guys) here is what I found:

The recommended way for achieving transparency is to set the desired
property values from the constructor of the custom control (inherited
from ...Forms.Label in your case). Unfortunately no combination of
styles will make the control transparent. (If someone knows a winning
combination PLEASE tell me).

What I used in my code is the following:

Public Class TransparentLabel
Inherits System.Windows.Forms.Label


Public Sub New()
MyBase.New()
SetStyle(ControlStyles.DoubleBuffer, False)
End Sub


Protected Overrides ReadOnly Property CreateParams() As
CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams()
cp.ExStyle = cp.ExStyle Or 32
CreateParams = cp
End Get

End Property

End Class

....
lbl = New TransparentLabel()
lbl.BackColor = Color.Transparent

MyPanel.Controls.Add(lbl)
....

The double buffering need to be disabled otherwise you will have
problems the first time the label paints itself and also if you scroll
the container control.
I hope this helps.

Iassen
 
T

Tony

Hi Iassen

This sounds exactly what I'm looking for, the probelm is
could you provide me with a c# version of the code you
posted.

Thanks
Tony
 
P

Paul

My appologies - transparency wasn't really my issue. It was the need to
make the backcolor the same as the form - just missed it in the property
list.... repeatedly! Thanks for waving it under me nose :blush:)
 
P

Paul

I take it back... kind of. Was having other isses and discovered I'm
stillrunning SP2. After doing a full reset on the PDA and patching to
SP1, the backcolor property has no effect...

Paul
 
G

Geoff Schwab [MSFT]

This is a known issue that will be fixed once SP2 has been re-released.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Theorin

..NET Compact Framework solution?

I guess I don't understand this anyway... doesn't seem to support true
transparency on a full framework solution either, just BackColor (even with
a 'SetStyle(ControlStyles.SupportsTransparentBackColor, True)' line added to
the 'New() sub').
 

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