使用BorderColor属性创建自定义标签

时间:2016-10-02 11:18:18

标签: vb.net user-controls

我尝试使用BorderColor属性创建标签。但它不起作用。我在表单应用程序中创建了这个标签的即时对象,并尝试更改BorderColor,但什么也没发生。 这是我的代码:

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

结束班

2 个答案:

答案 0 :(得分:1)

不,不,不是否。你不要打电话给GraphicsPaint事件为您提供了Paint对象,您可以使用它。此外,您无法在自定义控件中处理OnPaint事件,而是覆盖CreateBorder方法。此外,如果您有一个绘制方法,例如OnPaint,那么除了Invalidate方法之外,你不要从任何地方调用它。如果您想确保在下一个活动中重新绘制边框,请致电Public Class BorderedLabel Inherits Label Private _borderColor As Color Property BorderColor As Color Get Return _borderColor End Get Set _borderColor = Value Invalidate() End Set End Property Protected Overrides Sub OnPaint(e As PaintEventArgs) MyBase.OnPaint(e) Using p As New Pen(BorderColor, 2) e.Graphics.DrawRectangle(p, DisplayRectangle) End Using End Sub End Class 。 E.g。

Url.Action("Edit", "Edit", new { id = item.UserID })

答案 1 :(得分:-1)

谢谢......现在可以了 这是新代码:

Public Class MyLabel
Inherits Label

Private _BorderColor As Color
Private _BorderSize As Single = 1.0F

Public Property BorderColor As Color
    Get
        Return _BorderColor
    End Get
    Set(value As Color)
        _BorderColor = value
        Refresh()
    End Set
End Property

Public Property BorderSize As Single
    Get
        Return _BorderSize
    End Get
    Set(value As Single)
        _BorderSize = value
    End Set
End Property

Private Sub CreateBorder(ByVal value As Color, ByVal e As PaintEventArgs)
    Dim g As Graphics = e.Graphics
    Dim p As Pen = New Pen(value, _BorderSize)
    g.DrawRectangle(p, Me.DisplayRectangle)
End Sub

Private Sub MyLabel_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    CreateBorder(_BorderColor, e)
End Sub

结束班