Transparent Labels

Brainbug

Member
Joined
Dec 29, 2005
Messages
17
Location
Hanover, Germany
Programming Experience
5-10
Hi,

I want to put a transparent label on top of a progress bar (white font, so it will be white on blue when the progress goes on).

Even with the progress bar as parent of the label, the gray label-background is still shown.

Any suggestions about that one?

Thank you,

Brainbug
 
set the label's BackGround property to Transparent
in code it's
Label1.BackColor = Color.Transparent

and then:
Label1.ForeColor = Color.White
 
you can make the label transparent by lbl.backcolor=somecolor

then make the traparency key of the form to that somecolor.

the label is transparent
 
Not as simple!

Hi Guys,

thanks for the quick replys. Of course I tried all that. If it was so simple I would not have posted this: look at this screenshot

trans_fail.gif


The label (BckColor = Color.Transparent) is above the progress bar, and it IS transparent allright, but only to the gray background of the progress bar, not to the blue bars on it.

Any ideas now?

Appreciate your help,

Brainbug

(PS: The label in the picture is enabled=false, but there is no difference when it's enabled, just the text is white then.)
 
You should either inherit the ProgressBar and handle the painting your self or else use GDI+ to draw over the top of your ProgressBar. If you don't mind using a third-party control and you don't need theme support, you could look at the DoneOMeter in the ElementsEx library (see my sig).
 
What to choose...

As I would like to keep my application light weight I'd actually prefer to get along without third-party controls if possible.

So I would like to try one of the other ideas of yours, jmcilhinney. The problem is: I have no idea how to handle the painting once I have overridden the paint-method or whatever. Some advise about that?

Thanks in advance,

Brainbug

PS: How can it be so complicated to make a label transparent? In VB6 it was no problem at all!
 
To over come the painting events research making round or triangle buttons or controls. But basically I can teach you. Send me an e-mail and I will send you a code snippet I have written that solves your problem. I will try to post this code later b/c I am not on my home computer. Apologies. But basically the easiest way I know to do it is build a class that inherits from the control progressbar. Then override it's on paint event. There is an e argument that I can't remember the name of right now but it handles the graphics for the progress bar. Simply add to it by drawing text. It will run like this.

Overrides OnPaint
MyBase.OnPaint(sender, e)
declare p as new pen for drawing text.....
Draw the text over the pgrogressbar
End sub
I will send a working snippet later. Apologies.
 
Ok, I have tried to make the progress bar for you and unfortunately ProgressBar is declared UnInheritable so I can't. I recommend getting a third party control. If you are going to cut and paste code there is no difference. I have done what I recommended with the button class and assumed that all classes were inheritable. There may be an easier way but you may also consider just building a progress bar with GDI+. This may take longer but it will have the look you gave it and will work exactly like you want it to.
 
Ok, I went far out of my way to make you a progress bar that works with text in it. You can use this progress bar on the ide to see it in action. Add and take what you want from it and learn from it. This will do exactly what you want and now with the source code you can influence it to achieve more. Note: this progress bar does not look like the xp one in the ide. This one actually increments more smooth and if you want to make changes then experiment and have at it. :) This is the best thing about programming. Creativity :)

Imports System.Windows.Forms
PublicClass LabelProgressBar
Inherits UserControl
#
Region "Private Declarations"
Private m_MaxValue AsInteger = 1000
Private m_Value AsInteger = 0
Private m_BarColor As Color
Private m_Label AsString = "Testing this"
#
EndRegion
#Region "Events"
Event MaxValue_Changed(ByVal sender AsObject, ByVal e As System.EventArgs)
Event Value_Changed(ByVal sender AsObject, ByVal e As System.EventArgs)
Event Label_Changed(ByVal sender AsObject, ByVal e As System.EventArgs)
Event BarColor_Changed(ByVal sender AsObject, ByVal e As System.EventArgs)
#
EndRegion
Property MaxValue() AsInteger
Get
ReturnMe.m_MaxValue
EndGet
Set(ByVal Value AsInteger)
Me.m_MaxValue = Value
Me.Invalidate()
RaiseEvent MaxValue_Changed(Me, Nothing)
EndSet
EndProperty
Property BarColor() As Color
Get
ReturnMe.m_BarColor
EndGet
Set(ByVal Value As Color)
Me.m_BarColor = Value
Me.Invalidate()
RaiseEvent BarColor_Changed(Me, Nothing)
EndSet
EndProperty
Property Value() AsInteger
Get
Return m_Value
EndGet
Set(ByVal Value AsInteger)
If Value > Me.MaxValue Then Value = Me.MaxValue
Me.m_Value = Value
Me.Invalidate()
RaiseEvent Value_Changed(Me, Nothing)
EndSet
EndProperty
Property Label() AsString
Get
Return m_Label
EndGet
Set(ByVal Value AsString)
m_Label = Value
Me.Invalidate()
RaiseEvent Label_Changed(Me, Nothing)
EndSet
EndProperty
ProtectedOverridesSub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim W AsInteger = Me.Width * (Value / MaxValue)
Dim b As SolidBrush = New SolidBrush(Me.BarColor)
If W <> 0 Then e.Graphics.FillRectangle(b, 0, 0, W, Me.Height)
b.Color =
Me.ForeColor
e.Graphics.DrawString(
Me.Label, MyBase.Font, b, 0, 0)
EndSub
End
Class

Note: The baseClass has it's own Text property but it was acting kinda glitchy even when shadowing it so i made the new one called Label.

How to use this progress bar... follow this carefully for now... There are better ways but this is the quickest...

Drag a button onto your form. Changed the name of the button to ProgresBar1 or whatever you want. Then open the windows generated code in the IDE. Find the declarations for the button and change it from this..
FriendWithEvents ProgressBar1 As System.Windows.Forms.Button
to this...
FriendWithEvents ProgressBar1 As LabelProgressBar

do this again once more in the component declaration section a lil lower in the windows generated code. THen go back to the IDE and size the control. Change the backcolor, label text, font size, font color, BarColor, ect directly on the IDE. I even added events for you to use in your code if needs be. I did this in 15 min so there are many more possibilities for you to work with. Have fun and please let me know how it goes.
 
Back
Top