Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to make an application where the user can draw an electric installation of a building. My plan to do so is generating a label when an electric part is selected.
This label is generated inside a panel, but when i generate two labels for example, they overlap instead of being transparent to each other. Is there a way to make those generated labels transparent to each other?

Here is my code for generating a label with a buttonclick:
(the code is running in vb.net)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim lbl As New Label With {
            .Visible = True,
            .Text = (++lblNum).ToString(),
            .AutoSize = True,
            .Font = New Font(System.Drawing.FontStyle.Bold, 15)
        }
        'lbl.BackColor = Color.Transparent --> doesn't work
        Panel2.Controls.Add(lbl)

        AddHandler lbl.MouseDown, AddressOf LabelMouseDown
        AddHandler lbl.MouseMove, AddressOf LabelMouseMove
        AddHandler lbl.MouseUp, AddressOf LabelMouseUp

        lblNum = lblNum + 1

    End Sub


Thanks in advance.

Greetings,
Rudy

What I have tried:

I tried to make the background of the label transparent but that doesn't seem to work. Also when I searched for the problem, I came across the method to make a picturebox as a parent of the label. I tried this mechanic with the label itself, but obviously a control can't be a parent of itself.
Posted
Updated 5-Apr-18 21:47pm

1 solution

Quote:
By default, controls do not support transparent backcolors. However, you can allow your control to have a background color that is opaque, transparent, or partially transparent by using the SetStyle method[^] in the constructor. The SetStyle method of the Control class allows you to set particular style preferences for your controls, and can be used to enable or disable support for transparent backcolors.
NoteNote

Note:
Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent.

To give your control a transparent backcolor

Locate the constructor for your control class. The constructor appears in the control's code file. In C#, the constructor is the method with the same name as the control and with no return value. In Visual Basic, the constructor is the method named New.

Call the SetStyle method of your form in the constructor
VB
SetStyle(ControlStyles.SupportsTransparentBackColor, True)



Source: How to: Give Your Control a Transparent Background[^]


[EDIT]
Due to SetStyle accessibility (protected method[^]), the only way to create transparent Control (in this case Label) is to create custom class which derives[^] from Control (or Label), for example:
Public Class TransparentLabel
	Inherits Label
		
	Public Sub New
		Me.ForeColor = Color.Green
		Me.BorderStyle = 1 'Solid. The border is centered on the edge of the shape.
		Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
	End Sub
 
End Class

Then, you have to create custom controls in code:
VB
Dim tl As TransparentLabel = New TransparentLabel()
With tl
    .Location = New System.Drawing.Point(10, 10)
    .Parent = Me 'Form
   'Set other properties here
End With


Note: not tested (haven't enough time).
 
Share this answer
 
v3
Comments
TheRuudster 6-Apr-18 4:52am    
Dim lbl As New Label With {
.Visible = True,
.Size = New System.Drawing.Size(50, 50),
.SetStyle(ControlStyles.SupportsTransparentBackColor, True),
.BackColor = Color.Transparent
}

i'm stuck with the setstyle not being a property of the label control.

Could you give an example please?
Maciej Los 6-Apr-18 5:08am    
Please, read the documentation carefully! This statement is very important: Locate the constructor for your control class.
Steps to do:
1. View code of your Form
2. On the right-top corner of code-pane, you'll find a combobox (by default it displays: "(Declarations)"). Select "New". This will create Public Sub New of that form.
3. In the body of above procedure you'll find 3 lines of code:
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.

4. Move mouse cursor over InitializeComponent(), right-click and choose: Go To Definition (F12). You'll be moved into Form.Designer.vb file
5. Inside Private Sub InitializeComponent() procedure you have to change transparency for controls ;)

Good luck!
Maciej Los 6-Apr-18 5:18am    
Oooops!
I forgot, that you create controls in code...
I'll try to give you working example.
Please, be patient.
TheRuudster 6-Apr-18 5:35am    
Thanks haha, I wasn't familiar with the designer code at all, so that is nice to know! But whenever you add code to the designer, the design becomes inoperable :(
Maciej Los 6-Apr-18 6:42am    
See updated answer ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900