Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Label.BackColor transparent

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,509
US
VB.NET 2010
I create a label in the code and place it on the panel (pnlTop) on the Windows Form:

Code:
Dim lbl As New Label

With lbl
    .Name = "lblNewLabel" 
    .Text = "Some text here"

    Me.pnlTop.Controls.Add(lbl)

    .Left = 20
    .Top = 50
    .AutoSize = True[red]
    .BackColor = Color.Transparent   [/red]
    .BringToFront()
End With

Sometimes the newly created label is placed over another control on the panel and I would like the label to have transparent back color (red line of code) and not the default gray background. But the red line of code is ignored and I still see the gray background of the label.

Is there any way to change the back color of the label to transparent in the code?


Have fun.

---- Andy
 
Does the version of Windows you're running on support transparency? I tried your sample and it works okay on my system?
 
Thank you Dave for trying...

I have Windows 7

When I place the label at design time, it looks like its BackColor is transparent by default. Interesting...

So what I tried at design time was:
Placed a big Label2 on the Form, AutoSize to False, send it to back
Placed a small Label1 on top of Label2, bring it to front

3 buttons:

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        Me.Label2.BackColor = Color.Red
        Me.Label1.Text = "This is color red"[red]
        Me.Label1.BackColor = Color.Transparent[/red]
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button2.Click
        Me.Label2.BackColor = Color.Blue
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button3.Click
        Me.Label2.BackColor = Color.Green
    End Sub

Red line of code - ignored :-(


Have fun.

---- Andy
 
It appears that Color.Transparent isn't really transparent but rather the color of the control's container. That's why I had originally though it was working correctly on my system as I had placed it in a Panel. I'm getting the same results as you with your example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top