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