在自定义TextBox控件周围绘制矩形

时间:2014-07-05 22:12:51

标签: vb.net custom-controls drawrectangle

我正在尝试创建一个自定义TextBox控件,它将具有另外两个属性BorderColor和BorderSize。但即使BorderStyle设置为None,边框也不会显示出来。 这是我的代码:

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

0 个答案:

没有答案