Drawing Rectangle around Custom TextBox control

2019-08-27 02:03发布

问题:

I'm trying to create a custom TextBox control that will have two additional properties BorderColor and BorderSize. But the border doesn't shows up, even with BorderStyle set to None. Here is my code:

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


Public Class BorderedTextBox
    Inherits TextBox

    Dim BColor As Color = Color.Black
    Dim BSize As Double = 1.0

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



    Public Property BorderSize As Double
        Get
            Return BSize
        End Get
        Set(value As Double)
            BSize = value
        End Set
    End Property

    Public Sub New()
        SetStyle(ControlStyles.UserPaint, True)

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim g As Graphics = e.Graphics

        Dim pen As New Pen(BColor, BSize)
        g.DrawRectangle(pen, New Rectangle(Me.Location, Me.Size))
        pen.Dispose()

        MyBase.OnPaint(e)
    End Sub

End Class