Our Featured Articles:
1 2 3 4 5 6 7 8 9 10


Creating a real transparent label in vb.net

November 30, 2011 admin Classes

Post to Twitter




Creating a true transparent label.

  • Requirements: Visual Studio 2010
  • Programming Level: Advance
  • Language: Visual Basic.net

Scenario:

I have been looking for quite some time a control that can give me a true transparent label in which we can see the background color or object behind this control just like the old VB6 language, by just setting up the BackStyle Property of VB6 Label Control. As Shown Below:

Microsoft Visual Basic 6 Label Control

Microsoft Visual Basic 6 Label Control

But unfortunately in visual basic.net it is not possible as visual basic.net treated this in different way, it only copy the background color of the form and really making it a true transparent. Well in this article I am going to show you how i was able to create a true transparent label in visual basic.net.  You need to create a new project in Visual Studio 2010 and add another project in your solution and select Class Library as project Type.  Then add a user control in this Class Library Project. The Code is given below:

You shall need to add These References in your Class Project:

System.Drawing
System.Windows.Forms
System.ComponentModel

The Source Code


Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Public Class TransparentLabel
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.Opaque, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.components = New System.ComponentModel.Container()
        RF = New RectangleF(0, 0, MyBase.Width, MyBase.Height)
        LabelForeColorBrush = New SolidBrush(MyBase.ForeColor)
    End Sub
#Region "Private"
    Private sFormat As StringFormat
    Private RF As RectangleF
    Dim LabelForeColorBrush As SolidBrush = Nothing
#End Region
#Region "Subroutines"
    Private Sub UpdateText()
        Try
            sFormat = New StringFormat
            Dim x As Integer = 0
            Dim y As Integer = 0
            With sFormat
                Select Case TextAlignment
                    Case ContentAlignment.BottomCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.BottomLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.BottomRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Far
                    Case ContentAlignment.MiddleLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.MiddleCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.MiddleRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Center
                    Case ContentAlignment.TopCenter
                        sFormat.Alignment = StringAlignment.Center
                        sFormat.LineAlignment = StringAlignment.Near
                    Case ContentAlignment.TopLeft
                        sFormat.Alignment = StringAlignment.Near
                        sFormat.LineAlignment = StringAlignment.Near
                    Case ContentAlignment.TopRight
                        sFormat.Alignment = StringAlignment.Far
                        sFormat.LineAlignment = StringAlignment.Near
                End Select
                .FormatFlags = StringDirection
            End With
            ResizeControl()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub ResizeControl()
        RF.Size = New Size(MyBase.Size)
        Invalidate()
    End Sub
#End Region
#Region "Properties"
    Dim _StringDirection As StringFormatFlags = StringFormatFlags.NoClip
    <Description("The Direction of the Text."), DefaultValue(StringFormatFlags.NoClip)>
    Public Property StringDirection As StringFormatFlags
        Get
            Return _StringDirection
        End Get
        Set(ByVal value As StringFormatFlags)
            _StringDirection = value
            UpdateText
        End Set
    End Property

    Private _TextAlignment As System.Drawing.ContentAlignment = ContentAlignment.MiddleCenter
    <Description("The Text Alignment that will appear on this control."), DefaultValue(ContentAlignment.MiddleCenter)>
    Public Property TextAlignment() As System.Drawing.ContentAlignment
        Get
            Return _TextAlignment
        End Get
        Set(ByVal value As System.Drawing.ContentAlignment)
            _TextAlignment = value
            UpdateText()
        End Set
    End Property

#End Region
#Region "Overrides"
    Public Overrides Property ForeColor As System.Drawing.Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.ForeColor = value
            LabelForeColorBrush = New SolidBrush(value)
        End Set
    End Property
    ''' <summary>
    ''' The text to be displayed in supports with real transparency.
    ''' </summary> 
    ''' <remarks></remarks>
    Dim _Labeltext As String = "TransparentLabel"
    <Description("The text to be displayed in supports with real transparency."), Category("Text"), DefaultValue("TransparentLabel")>
    Public Property LabelText As String
        Get
            Return _Labeltext
        End Get
        Set(ByVal value As String)
            _Labeltext = value
            Invalidate()
        End Set
    End Property

    <Browsable(False), EditorBrowsable(False)>
    Public Overrides Property BackColor As System.Drawing.Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
        End Set
    End Property
    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20
            Return cp
        End Get
    End Property
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Try
            MyBase.OnPaint(e)
            ' draw the text on the control
            e.Graphics.DrawString(LabelText, MyBase.Font, LabelForeColorBrush, RF, sFormat)
            ' MyBase.OnPaint(e)
        Catch ex As Exception

        End Try

    End Sub
#End Region
    Private Sub TransparentLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        ResizeControl()
    End Sub
End Class

The Screenshot is given below for the transparent control:

The true transparent label control in vb.net

The true transparent label control in vb.net

The Full Source Code Project Download in VB.NET and in C#.NET

You can download the full sourcecode Here in ZIP Format but please like our Facebook Fan Page first.

Or for c# enthusiast you can try my C# equivalent TransparentLabel in C Sharp Here.

Please use the password below if asked for a password during extraction:

Password: ilikesourcehints

VN:F [1.9.6_1107]
Rating: 10.0/10 (9 votes cast)
VN:F [1.9.6_1107]
Rating: +4 (from 4 votes)

Creating a real transparent label in vb.net, 10.0 out of 10 based on 9 ratings


Post to Twitter

Keywords: , , , , , , , , ,

Other reading this article are also reading these:

classes, ComponentModel, imports, oop, programming,

18 comments on “Creating a real transparent label in vb.net

  1. Joaquim on said:

    i’m trying use your sample, but i can’t:(
    something to do with DotBarNet2(somehting like these).
    can you advice me?
    thanks

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: +1 (from 1 vote)
    • admin on said:

      Joaquim:

      I have already fixed the problem you may download it again..

      thanks!

      VN:F [1.9.6_1107]
      Rating: 5.0/5 (1 vote cast)
      VN:F [1.9.6_1107]
      Rating: +1 (from 1 vote)
  2. Chris on said:

    Is there anything that is specific to VB2010 or will it translate to VB2008 ok?
    To the limit of my knowledge, I did not see anything I recognised as alien to VB2008.
    Thanks

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  3. Kyle Cabeltes Sunico on said:

    This is a true transparent control in vb.net…

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: +1 (from 1 vote)
  4. joaquim on said:

    sorry… these isn’t a real transparent:(
    if is a real transparent, why i can’t click in button close to the text?
    sorry.. but i tested

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: +1 (from 1 vote)
  5. Nolan Sunico on said:

    Joaquim:

    yap it is indeed a real transparent label that is why you cannot click the button because the button is behind this label, in other words the label is in front the button. you can see the button because it does contained a transparent properties that enable you to see controls behind on it.

    VN:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  6. Nolan Sunico on said:

    A TRANSPARENT means: allowing light to pass through with little or no interruption or distortion so that objects on the other side can be clearly seen, meaning you can only see but you cannot access it.[Encarta] The transparency of this label follows the property of a MIRROR itself. in which you can only see things behind the mirror but you cannot touch anything behind it.

    VN:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  7. Thank you from your useful post.
    could you prepare this code in C#?

    Best Regards …

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
    • Nolan Sunico on said:

      Saman;
      Yap why not, I just written it in vb cause vb is my first windows application language i have learned not to mention Visual Foxpro early in 2002 and then I migrated to vb6, now to vb.net, since that c# is a high breed language a combinatination of vb and c++ paradigm a vb programmer can easily adopt c# as well. thanks for your comment I do appreciate it.

      VN:F [1.9.6_1107]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.6_1107]
      Rating: +1 (from 1 vote)
      • Thank you for your reply.i need this.even i Ask a question for this :
        http://stackoverflow.com/questions/14932063/convert-code-of-a-user-control-from-vb-net-to-c-sharp

        Kind Regards …

        VA:F [1.9.6_1107]
        Rating: 0.0/5 (0 votes cast)
        VA:F [1.9.6_1107]
        Rating: 0 (from 0 votes)
  8. I Converted your Control to C# but it has the problem.
    I want to make this control Movable by user.
    and i add this codes :

    Point LastPositionOfLetterAmount = new Point();
    private void transparentLabel2_MouseDown(object sender, MouseEventArgs e)
    {
    LastPositionOfLetterAmount = e.Location;
    transparentLabel2.Cursor = Cursors.SizeAll;
    }

    private void transparentLabel2_MouseMove(object sender, MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Left)
    {
    transparentLabel2.Top += e.Location.Y – LastPositionOfLetterAmount.Y;
    transparentLabel2.Left += e.Location.X – LastPositionOfLetterAmount.X;
    }
    }

    transparentLabel2 is the instance of your control.but after moving label,it isn’t transparent anymore and it like a normal label.what should i do for doing this?

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: +1 (from 1 vote)
    • Nolan Sunico on said:

      Saman;

      I have converted my vb.net transparent label control to equivalent c#, you can try this…thanks for following my articles.

      VN:F [1.9.6_1107]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.6_1107]
      Rating: 0 (from 0 votes)
  9. any previous text is not cleared when new text is entered. e.graphics.clear(me.parent.backcolor) does not work. Any ideas?

    VA:F [1.9.6_1107]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.6_1107]
    Rating: +1 (from 1 vote)
    • Nolan Sunico on said:

      JG:

      plz execute the refresh method of the object control for the changes to take effect, thanks for you comments.

      VN:F [1.9.6_1107]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.6_1107]
      Rating: 0 (from 0 votes)
  10. The transparent label control is really great. however, this line of code
    e.Graphics.Clear(BackColor)
    needs to added to the OnPaint method to remove any exisiting text before drawing any new text.

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  11. Donna on said:

    Isn’t that amazing that all that massive/complex code is needed? You just can’t set the Transparent property to True.

    Ugh. Thanks, Microsoft.

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  12. Joaquim on said:

    just 1 question:
    – can you do Opacy with these control?

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  13. whocares on said:

    I’ve added text, and textalign overrides giving us the option to replace all designer codes with a simple replace from normal label to transparent control. Every setting stays the same. This is pretty awesome code thank you. Took me 2 days to find this.
    Public Overrides Property Text As String
    Get
    Return _Labeltext
    End Get
    Set(ByVal value As String)
    _Labeltext = value
    Invalidate()
    End Set
    End Property

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

79,497 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

Powered by WordPress 3.5.1. Designed by elogi. Allright Reserved SourceHints 2010-2014