none
create a custom label with BorderColor property RRS feed

  • Question

  • hi friends
    I try to create a label with BorderColor property.but it not work . I create an instant object of this label in my form application and try to change the BorderColor , but nothing is happen.
    this is my code
    Public Class MyLabel
        Inherits Label
    
        Private _BorderColor As Color
        Dim e As New PaintEventArgs(Me.CreateGraphics, Me.DisplayRectangle)
    
        Public Property BorderColor As Color
            Get
                Return _BorderColor
            End Get
            Set(value As Color)
                _BorderColor = value
                CreateBorder(value)
            End Set
        End Property
    
        Private Sub CreateBorder(ByVal value As Color)
            Dim g As Graphics = Me.CreateGraphics
            Dim p As Pen = New Pen(value, 2)
            g.DrawRectangle(p, Me.DisplayRectangle)
        End Sub
    
        Private Sub MyLabel_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            CreateBorder(_BorderColor)
        End Sub
    End Class


    Sunday, October 2, 2016 11:12 AM

Answers

  • Try this class:

    Public Class MyLabel
        Inherits Label
    
        Private _BorderColor As Color
    
        Public Property BorderColor As Color
            Get
                Return _BorderColor
            End Get
            Set(value As Color)
                _BorderColor = value
                Invalidate()
            End Set
        End Property
    
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
    
            Using p = New Pen(_BorderColor, 2)
                e.Graphics.DrawRectangle(p, Me.ClientRectangle)
            End Using
        End Sub
    
    End Class

    • Edited by Viorel_MVP Sunday, October 2, 2016 11:58 AM
    • Proposed as answer by IronRazerz Sunday, October 2, 2016 12:40 PM
    • Marked as answer by Neda ZhangModerator Thursday, October 13, 2016 3:15 AM
    Sunday, October 2, 2016 11:55 AM

All replies

  • Try this class:

    Public Class MyLabel
        Inherits Label
    
        Private _BorderColor As Color
    
        Public Property BorderColor As Color
            Get
                Return _BorderColor
            End Get
            Set(value As Color)
                _BorderColor = value
                Invalidate()
            End Set
        End Property
    
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
    
            Using p = New Pen(_BorderColor, 2)
                e.Graphics.DrawRectangle(p, Me.ClientRectangle)
            End Using
        End Sub
    
    End Class

    • Edited by Viorel_MVP Sunday, October 2, 2016 11:58 AM
    • Proposed as answer by IronRazerz Sunday, October 2, 2016 12:40 PM
    • Marked as answer by Neda ZhangModerator Thursday, October 13, 2016 3:15 AM
    Sunday, October 2, 2016 11:55 AM
  • Also see Fancy Custom Label Class (LabelEx) and this thread Custom Label Class (LabelEx) which has other code in it too.

    La vida loca

    Sunday, October 2, 2016 8:20 PM