none
Label backcolor is same as panel backcolor if setting backcolor of label to transparent RRS feed

  • Question

  • HI, I have created label and picturebox dynamically in a panel with back color = green, label back color is set to transparent but when I bring the label in front of picture box, label back color become similar to panel back color (GREEN) instead of showing color of picture box. Am I missing something?

      
    Sunday, November 17, 2019 2:31 AM

All replies

  • Ahh yes, you have come to the right place Mir. I believe you have stumbled on the well known and most frustrating problem with [I presume you are using] WinForms and their problem with transparencies.

     

    I can't explain it better, nor would I want to steal the wizened words from Rick Strahl - a fellow MVP and a prolific poster of the plethora of perturbing programming pitfalls!

     

    https://weblog.west-wind.com/posts/2008/feb/07/transparent-labels-in-winforms

     

    All the best,
    Pete

     


    #PEJL
    Got any nice code? If you invest time in coding an elegant, novel or impressive answer on MSDN forums, why not copy it over to TechNet Wiki, for future generations to benefit from! You'll never get archived again, and you could win weekly awards!

    Have you got what it takes to become this month's TechNet Technical Guru? Join a long list of well known community big hitters, show your knowledge and prowess in your favoured technologies!

    Sunday, November 17, 2019 3:24 AM
    Moderator
  • Here are some examples of what there is.

    If you cant use one of these then show an image and describe exactly of what you want to achieve maybe we will have ideas on how to do it.

    With Form TransparencyKey = Color.Lime, any control with backcolor = color.lime will show the form background ie desktop or another form.

    If the Form has no transparency defined then we see the label backcolor (color.lime in the example not shown).

    If the label has an image we see the image when not transparent (not shown).


    Shown in the example image with the form transparencykey = color.lime:

    Labels on form

      Label.Backcolor = Color.MistyRose     shows the label backcolor Rose.

      Label.Backcolor = Color.Transparent   shows the parent backcolor Turquoise.

      Label.Backcolor = Color.Lime          shows the background Image behind form.


    Labels on panel

      Label.Backcolor = Color.MistyRose     shows the label backcolor Rose.

      Label.Backcolor = Color.Transparent   shows the parent backcolor red.

      Label.Backcolor = Color.Lime          shows the background Image behind form.

    Public Class Form4
    
        Private WithEvents Label1 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 10), .Size = New Size(150, 40), .Text = "Rose",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.MistyRose}
    
        Private WithEvents Label2 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 70), .Size = New Size(150, 40), .Text = "Transparent",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.Transparent}
    
        Private WithEvents Label3 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 130), .Size = New Size(150, 40), .Text = "Lime",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.Lime}
    
        Private WithEvents Panel1 As New Panel With {.Parent = Me, .BackColor = Color.Red,
        .Location = New Point(40, 0), .Size = New Size(50, 200)}
    
        Private WithEvents Panel2 As New Panel With {.Parent = Me, .BackColor = Color.Red,
        .Location = New Point(200, 5), .Size = New Size(200, 200)}
    
        Private WithEvents Label4 As New Label With {.Parent = Panel2, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 10), .Size = New Size(150, 40), .Text = "Rose",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.MistyRose}
    
        Private WithEvents Label5 As New Label With {.Parent = Panel2, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 70), .Size = New Size(150, 40), .Text = "Transparent",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.Transparent}
    
        Private WithEvents Label6 As New Label With {.Parent = Panel2, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 130), .Size = New Size(150, 40), .Text = "Lime",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.Lime}
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(500, 300)
            BackColor = Color.Turquoise
            TransparencyKey = Color.Lime
            Panel1.SendToBack()
        End Sub
    End Class

    Sunday, November 17, 2019 12:43 PM
  • Hi,

    Do you want to get the following results?

    I think that Tommy offers a great solution, you can modify his code as follows:

    Public Class Form1
    
        Private WithEvents Panel3 As New Panel With {.Parent = Me, .BackColor = Color.Green,
        .Location = New Point(10, 15), .Size = New Size(200, 200)}
    
        Private WithEvents picturebox1 As New PictureBox With {.Parent = Panel3, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 10), .Size = New Size(180, 180),
            .BackColor = Color.Red}
    
        Private WithEvents Label9 As New Label With {.Parent = picturebox1, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(10, 20), .Size = New Size(150, 40), .Text = "Transparent",
            .Font = New Font("tahoma", 14), .ForeColor = Color.Black,
            .BackColor = Color.Transparent}
    
    End Class

    Hope to help you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 18, 2019 6:18 AM
    Moderator