Click here to Skip to main content
13,295,178 members (68,875 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 8 Jul 2010

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

You may also be interested in...


Comments and Discussions

QuestionTransparent Label With Checkbox Pin
gwittlock24-Mar-13 4:53
membergwittlock24-Mar-13 4:53 
AnswerRe: Transparent Label With Checkbox Pin
AlphaBeta011027-Apr-13 22:06
memberAlphaBeta011027-Apr-13 22:06 
QuestionLabels Flickering On Update - Not Double buffering? Pin
mtiminer8-Mar-13 16:05
membermtiminer8-Mar-13 16:05 
QuestionThanks Much Pin
mtiminer8-Mar-13 5:54
membermtiminer8-Mar-13 5:54 
QuestionWrap Text Pin
Member 81732524-Sep-12 23:42
memberMember 81732524-Sep-12 23:42 
Hi this is just what I needed.

How can I get the label to wrap the text when its longer than the label size though?
GeneralMy vote of 3 Pin
GeekForChrist9-Jul-12 4:04
memberGeekForChrist9-Jul-12 4:04 
QuestionAny C# version? Pin
Pudjo9-Jun-12 17:27
memberPudjo9-Jun-12 17:27 
QuestionIs this control for .Net CF? Pin
Member 29596325-Sep-10 20:40
memberMember 29596325-Sep-10 20:40 
AnswerRe: Is this control for .Net CF? Pin
Koshy Panicker John9-Sep-10 6:54
memberKoshy Panicker John9-Sep-10 6:54 
GeneralSmartDevice Pin
PabloDKP18-Aug-10 8:03
memberPabloDKP18-Aug-10 8:03 
GeneralRe: SmartDevice Pin
Koshy Panicker John18-Aug-10 8:43
memberKoshy Panicker John18-Aug-10 8:43 
GeneralRe: SmartDevice Pin
PabloDKP18-Aug-10 9:27
memberPabloDKP18-Aug-10 9:27 
GeneralRe: SmartDevice Pin
Koshy Panicker John18-Aug-10 10:16
memberKoshy Panicker John18-Aug-10 10:16 
Generalhi Pin
MegaGigsLtd17-Aug-10 15:25
memberMegaGigsLtd17-Aug-10 15:25 
GeneralRe: hi Pin
Koshy Panicker John17-Aug-10 22:27
memberKoshy Panicker John17-Aug-10 22:27 
General[My vote of 2] Good work but not much point Pin
The Man from U.N.C.L.E.19-Jul-10 23:00
memberThe Man from U.N.C.L.E.19-Jul-10 23:00 
GeneralThanks Pin
babu7415-Jul-10 3:50
memberbabu7415-Jul-10 3:50 
GeneralMy vote of 2 Pin
Adrian Cole8-Jul-10 12:11
memberAdrian Cole8-Jul-10 12:11 
Generaldownload link is not working Pin
vcop258-Jul-10 6:33
membervcop258-Jul-10 6:33 
GeneralRe: download link is not working Pin
Koshy Panicker John8-Jul-10 6:59
memberKoshy Panicker John8-Jul-10 6:59 
GeneralMy vote of 1 Pin
digital man8-Jul-10 0:11
memberdigital man8-Jul-10 0:11 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 9 Sep 2010
Article Copyright 2010 by Koshy Panicker John
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid