Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How do you create a transparent label on a gradient colored form?

How do you create a transparent label on a gradient colored form?

Scheduled Pinned Locked Moved Visual Basic
regexhelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David M J
    wrote on last edited by
    #1

    Hi, I am trying to create a form with a gradient backcolor with labels on it. I get the gradient back color of the form by overiding the onpaint event of the form. However when I put a label with color.transparant on the form I just get the backcolor of the form, system grey. I found a coulpe of aricles about gradien labels, however then you get the problem that the gradient of the label doesn't match the gradient of the form. Is there a way to get some labels on a form that at least look transparent? Thanks in Advance DJ Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

    T 1 Reply Last reply
    0
    • D David M J

      Hi, I am trying to create a form with a gradient backcolor with labels on it. I get the gradient back color of the form by overiding the onpaint event of the form. However when I put a label with color.transparant on the form I just get the backcolor of the form, system grey. I found a coulpe of aricles about gradien labels, however then you get the problem that the gradient of the label doesn't match the gradient of the form. Is there a way to get some labels on a form that at least look transparent? Thanks in Advance DJ Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

      T Offline
      T Offline
      The Man from U N C L E
      wrote on last edited by
      #2

      You new to create your own custom label which inherits from the .Net Label and allows Transparent Back Colours. Here is the simplest version of such a control.

      Public Class CustomLabel : Inherits Label

      Public Sub New()
          MyBase.New()
      
          Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
          Me.BackColor = Color.Transparent
      End Sub
      

      End Class

      Myself, I have a much more complex label I have created that supports TextElipsis, AutoHeight and so on. If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850)

      D 1 Reply Last reply
      0
      • T The Man from U N C L E

        You new to create your own custom label which inherits from the .Net Label and allows Transparent Back Colours. Here is the simplest version of such a control.

        Public Class CustomLabel : Inherits Label

        Public Sub New()
            MyBase.New()
        
            Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            Me.BackColor = Color.Transparent
        End Sub
        

        End Class

        Myself, I have a much more complex label I have created that supports TextElipsis, AutoHeight and so on. If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850)

        D Offline
        D Offline
        David M J
        wrote on last edited by
        #3

        Hi, Unfortunately this doesn't quite do the trick. With the backcolor being transparent, you get the backcolor of the form. However I am overriding the onpaint event of the form to create a gradient background (see code) Any other suggestions? Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics Dim myBrush As New LinearGradientBrush(ClientRectangle, Color.WhiteSmoke, _ Color.LimeGreen, LinearGradientMode.Horizontal) g.FillRectangle(myBrush, ClientRectangle) MyBase.OnPaint(e) End Sub thanks DJ Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

        T 1 Reply Last reply
        0
        • D David M J

          Hi, Unfortunately this doesn't quite do the trick. With the backcolor being transparent, you get the backcolor of the form. However I am overriding the onpaint event of the form to create a gradient background (see code) Any other suggestions? Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = Me.CreateGraphics Dim myBrush As New LinearGradientBrush(ClientRectangle, Color.WhiteSmoke, _ Color.LimeGreen, LinearGradientMode.Horizontal) g.FillRectangle(myBrush, ClientRectangle) MyBase.OnPaint(e) End Sub thanks DJ Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #4

          Odd... I have included the entire code of my test form here. When I run it I get the gradient applied uniformly even behind the label.

          Imports System.Reflection

          Public Class Form1
          Inherits System.Windows.Forms.Form

          #Region " Windows Form Designer generated code "

          Public Sub New()
              MyBase.New()
          
              'This call is required by the Windows Form Designer.
              InitializeComponent()
          
              'Add any initialization after the InitializeComponent() call
          
          End Sub
          
          'Form overrides dispose to clean up the component list.
          Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
              If disposing Then
                  If Not (components Is Nothing) Then
                      components.Dispose()
                  End If
              End If
              MyBase.Dispose(disposing)
          End Sub
          
          'Required by the Windows Form Designer
          Private components As System.ComponentModel.IContainer
          
          'NOTE: The following procedure is required by the Windows Form Designer
          'It can be modified using the Windows Form Designer.  
          'Do not modify it using the code editor.
          Friend WithEvents Label1 As CustomLabel
          Private Sub InitializeComponent()
              Me.Label1 = New CustomLabel
              Me.SuspendLayout()
              '
              'Label1
              '
              Me.Label1.Location = New System.Drawing.Point(136, 128)
              Me.Label1.Name = "Label1"
              Me.Label1.TabIndex = 0
              Me.Label1.Text = "Label1"
              '
              'Form1
              '
              Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
              Me.ClientSize = New System.Drawing.Size(292, 266)
              Me.Controls.Add(Me.Label1)
              Me.Name = "Form1"
              Me.Text = "Form1"
              Me.ResumeLayout(False)
          
          End Sub
          

          #End Region

          Public Shared Sub main()
              Application.EnableVisualStyles()
              Application.DoEvents()
              Application.Run(New Form1)
          End Sub
          
          Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
              MyBase.OnPaint(e)
          
              Dim brush As New Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.Azure, Color.DarkGreen, Drawing2D.LinearGradientMode.BackwardDiagonal)
              e.Graphics.FillRectangle(brush, Me.ClientRectangle)
              brush.Dispose()
          
          End Sub
          

          End Class

          Public Class CustomLabel : Inherits Label

          Public Sub New()
              MyBase.New()
          
              Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
          
          D 1 Reply Last reply
          0
          • T The Man from U N C L E

            Odd... I have included the entire code of my test form here. When I run it I get the gradient applied uniformly even behind the label.

            Imports System.Reflection

            Public Class Form1
            Inherits System.Windows.Forms.Form

            #Region " Windows Form Designer generated code "

            Public Sub New()
                MyBase.New()
            
                'This call is required by the Windows Form Designer.
                InitializeComponent()
            
                'Add any initialization after the InitializeComponent() call
            
            End Sub
            
            'Form overrides dispose to clean up the component list.
            Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                    If Not (components Is Nothing) Then
                        components.Dispose()
                    End If
                End If
                MyBase.Dispose(disposing)
            End Sub
            
            'Required by the Windows Form Designer
            Private components As System.ComponentModel.IContainer
            
            'NOTE: The following procedure is required by the Windows Form Designer
            'It can be modified using the Windows Form Designer.  
            'Do not modify it using the code editor.
            Friend WithEvents Label1 As CustomLabel
            Private Sub InitializeComponent()
                Me.Label1 = New CustomLabel
                Me.SuspendLayout()
                '
                'Label1
                '
                Me.Label1.Location = New System.Drawing.Point(136, 128)
                Me.Label1.Name = "Label1"
                Me.Label1.TabIndex = 0
                Me.Label1.Text = "Label1"
                '
                'Form1
                '
                Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                Me.ClientSize = New System.Drawing.Size(292, 266)
                Me.Controls.Add(Me.Label1)
                Me.Name = "Form1"
                Me.Text = "Form1"
                Me.ResumeLayout(False)
            
            End Sub
            

            #End Region

            Public Shared Sub main()
                Application.EnableVisualStyles()
                Application.DoEvents()
                Application.Run(New Form1)
            End Sub
            
            Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
                MyBase.OnPaint(e)
            
                Dim brush As New Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.Azure, Color.DarkGreen, Drawing2D.LinearGradientMode.BackwardDiagonal)
                e.Graphics.FillRectangle(brush, Me.ClientRectangle)
                brush.Dispose()
            
            End Sub
            

            End Class

            Public Class CustomLabel : Inherits Label

            Public Sub New()
                MyBase.New()
            
                Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            
            D Offline
            D Offline
            David M J
            wrote on last edited by
            #5

            Hi, Now it works. Made a mistake in the onpaint event... Thanks for the help !! Cheers DJ Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups