Click here to Skip to main content
Click here to Skip to main content
Go to top

Transparent Label .NET Control

, 9 Sep 2010
Rate this:
Please Sign up or sign in to vote.
This article explains how to create transparent Label controls in .NET Framework, uLabelX Transparent Label Control that support border styles, Images, and parent controls with gradient backgrounds


I think many people are having trouble creating a transparent label over a gradient background painted control.

This article illustrates an approach to showing transparent Labels in a Windows based application. uLabelX control in this library is designed to be flexible and stylish in its look. First of all, it supports border shapes, Images and parent controls with gradient backgrounds. Images you have associated with control can be drawn in Alpha Blended form. Texts in the control can be drawn with advanced effects. All these attractions can also be personalized for different states of the controls.


Using the Code

The project contains the following files:

  • BorderStyles.vb (Specifies the border style of the Control)
  • ImageSizes.vb (Specifies the Image Size of the Control)
  • uLabelXDesigner.vb (Specifies the class used to implement design-time services for the control)
  • uLabelX.vb (Specifies the custom Control Class)


The Control Class uLabelX Inherited from the base class Control.

Public Class uLabelX 
	Inherits Control 


Public Sub New()
	Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
	Me.SetStyle(ControlStyles.ResizeRedraw, True)
	Me.SetStyle(ControlStyles.Opaque, False)
	Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
		AddHandler Me.PropertyChanged, AddressOf OnPropertyChanged
		RaiseEvent PropertyChanged(False)
End Sub


Event Declaration
Protected Event PropertyChanged(ByVal _RecreateHandle As Boolean)
Method Invoked when Property Changed
Private Sub OnPropertyChanged(ByVal _RecreateHandle As Boolean) 
 	If (_RecreateHandle = True) Then Me.RecreateHandle() 
End Sub 



' Gets or sets the Image of the control 
Public Property Image() As Image 
		Return m_Image 
	End Get 
	Set(ByVal value As Image) 
		m_Image = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
' Gets or sets the ImageSize of the control 
Public ReadOnly Property ImageSize() As ImageSizes 
		Return m_ImageSize 
	End Get 
End Property 
' Gets or sets the BorderStyle of the control 
Public Shadows Property BorderStyle() _
	As iGreen.Controls.uControls.uLabelX.Common.BorderStyles 
		Return m_BorderStyle 
	End Get 
	Set(ByVal value As iGreen.Controls.uControls.uLabelX.Common.BorderStyles) 
		m_BorderStyle = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 


' Gets or sets the BorderDashStyle of the control 
Public Property BorderDashStyle() As System.Drawing.Drawing2D.DashStyle 
		Return m_BorderDashStyle 
	End Get 
	Set(ByVal value As System.Drawing.Drawing2D.DashStyle) 
		m_BorderDashStyle = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
' Gets or sets the BorderWidth of the control 
Public Property BorderWidth() As Single 
		Return m_BorderWidth 
	End Get 
	Set(ByVal value As Single) 
		m_BorderWidth = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
' Gets or sets the BorderColor of the control 
Public Property BorderColor() As Color 
		Return m_BorderColor 
	End Get 
	Set(ByVal value As Color)
	m_BorderColor = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 


' Gets or sets the Text of the control. 
' Represents Text as a series of Unicode characters. 
Public Overrides Property Text() As String 
		Return Replace(MyBase.Text, "ULabelX", "uLabelX") 
	End Get 
	Set(ByVal value As String) 
		MyBase.Text = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 
' Specifies alignment of Text on the control. 
' Placement and direction of text in relation to the control border 
Public Property TextAlign() As ContentAlignment 
		Return m_TextAlign 
	End Get 
	Set(ByVal value As ContentAlignment) 
		m_TextAlign = value 
		RaiseEvent PropertyChanged(True) 
	End Set 
End Property 


Here we are Overriding OnPaintBackground(), otherwise the background draw task spoils the recently repainted parent control content by crushing out the OnPaintBackground() method.

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs) 
End Sub 
Overriding the following to Refresh the control
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
	RaiseEvent PropertyChanged(True)
End Sub 
Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs)
	RaiseEvent PropertyChanged(True) 
End Sub
Overriding the base class OnPaint() to draw Control
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
	e.Graphics.CompositingMode = CompositingMode.SourceOver 
	e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected 
	e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 
		Call DrawControlBorder(e.Graphics, Me.ClientRectangle) 
		Call DrawControlText(e.Graphics, Me.ClientRectangle) 
		Call DrawControlImage(e.Graphics, Me.ClientRectangle) 
End Sub 

In order to remove the unnecessary properties from the designtime property page, here I’m using the ControlDesigner Class’s PostFilterProperties.

Imports System.Windows.Forms.Design
Namespace Common
    Friend Class uLabelXDesigner
        Inherits ControlDesigner
        Protected Overrides Sub PostFilterProperties_
	(ByVal _Properties As System.Collections.IDictionary)
        End Sub
    End Class
End Namespace

Hope this can be helpful.
Happy programming !!!


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


About the Author

Koshy Panicker John
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

QuestionTransparent Label With Checkbox Pinmembergwittlock24-Mar-13 3:53 
AnswerRe: Transparent Label With Checkbox PinmemberAlphaBeta011027-Apr-13 21:06 
QuestionLabels Flickering On Update - Not Double buffering? Pinmembermtiminer8-Mar-13 15:05 
QuestionThanks Much Pinmembermtiminer8-Mar-13 4:54 
QuestionWrap Text PinmemberMember 81732524-Sep-12 22:42 
GeneralMy vote of 3 PinmemberGeekForChrist9-Jul-12 3:04 
QuestionAny C# version? PinmemberPudjo9-Jun-12 16:27 
QuestionIs this control for .Net CF? PinmemberMember 29596325-Sep-10 19:40 
AnswerRe: Is this control for .Net CF? PinmemberKoshy Panicker John9-Sep-10 5:54 
GeneralSmartDevice PinmemberPabloDKP18-Aug-10 7:03 
GeneralRe: SmartDevice PinmemberKoshy Panicker John18-Aug-10 7:43 
Hi Dear
can you please try this..
Step 1 – In visual studio toolbox add your own tab or click on general tab.
Step 2 – Right click in the selected tab area and click Chose Items
Step 3 – This will open Choose Toolbox Item screen. under .Net Framwork Components Tab Click on Browse button and select the path of the iGreen.Controls.uControls.uLabelX.dll
Step 4 – Click Ok and you’re done. uLabelX control will be added to the toolbox
please try this and let me know..
GeneralRe: SmartDevice PinmemberPabloDKP18-Aug-10 8:27 
GeneralRe: SmartDevice PinmemberKoshy Panicker John18-Aug-10 9:16 
Generalhi PinmemberMegaGigsLtd17-Aug-10 14:25 
GeneralRe: hi PinmemberKoshy Panicker John17-Aug-10 21:27 
General[My vote of 2] Good work but not much point PinmemberThe Man from U.N.C.L.E.19-Jul-10 22:00 
GeneralThanks Pinmemberbabu7415-Jul-10 2:50 
GeneralMy vote of 2 PinmemberAdrian Cole8-Jul-10 11:11 
Generaldownload link is not working Pinmembervcop258-Jul-10 5:33 
GeneralRe: download link is not working PinmemberKoshy Panicker John8-Jul-10 5:59 
GeneralMy vote of 1 Pinmemberdigital man7-Jul-10 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 9 Sep 2010
Article Copyright 2010 by Koshy Panicker John
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid