实现功能:
a.ShowLine 参数:设置下划线是否可见
b.LineColor参数:设置下划线颜色
c.LineWidth参数:设置下划线粗细
d.LineOffset参数:设置下划线偏移量
Imports System.ComponentModel
<ToolboxBitmap(GetType(System.Windows.Forms.TextBox))>
Public Class TextBoxEx
Inherits System.Windows.Forms.TextBox
Protected _lineColor As Color = Color.Black
Protected _lineWidth As Integer = 1
Protected _lineOffset As Integer = 3
Protected _showLine As Boolean = False
Protected WithEvents Line As New Control()
<DefaultValue(GetType(Color), "Black")>
<Description("下划线颜色")>
Public Property LineColor As Color
Get
Return _lineColor
End Get
Set(value As Color)
If _lineColor <> value Then
_lineColor = value
RaiseEvent LineColorChanged(Me, Nothing)
End If
End Set
End Property
<DefaultValue(1)>
<Description("下划线宽度")>
Public Property LineWidth As Integer
Get
Return _lineWidth
End Get
Set(value As Integer)
If _lineWidth <> value Then
_lineWidth = value
RaiseEvent LineWidthChanged(Me, Nothing)
End If
End Set
End Property
<DefaultValue(3)>
<Description("下划线偏移量")>
Public Property LineOffset As Integer
Get
Return _lineOffset
End Get
Set(value As Integer)
If _lineOffset <> value Then
_lineOffset = value
RaiseEvent LineOffsetChanged(Me, Nothing)
End If
End Set
End Property
<DefaultValue(False)>
<Description("指示控件是否显示下划线")>
Public Property ShowLine As Boolean
Get
Return _showLine
End Get
Set(value As Boolean)
_showLine = value
RaiseEvent ShowLineChanged(Me, Nothing)
End Set
End Property
''' <summary>
''' 在 TextBoxEx.LineColor 属性值发生更改时发生
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Event LineColorChanged(ByVal sender As Object, e As EventArgs)
''' <summary>
''' 在 TextBoxEx.LineWidth 属性值发生更改时发生
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Event LineWidthChanged(ByVal sender As Object, e As EventArgs)
''' <summary>
''' 在 TextBoxEx.ShowLine 属性值发生更改时发生
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Event ShowLineChanged(ByVal sender As Object, e As EventArgs)
''' <summary>
''' 在 TextBoxEx.LineOffset 属性值发生更改时发生
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Event LineOffsetChanged(ByVal sender As Object, e As EventArgs)
Public Sub onLineWidthChanged(ByVal sender As Object, e As EventArgs) Handles Me.LineWidthChanged, Me.LineOffsetChanged, Me.LineColorChanged
If Me.ShowLine Then
rePosLine()
End If
End Sub
Public Sub onShowLineChanged() Handles Me.ShowLineChanged
If Me.ShowLine Then
BorderStyle = BorderStyle.None
BackColor = Color.FromKnownColor(KnownColor.Control)
rePosLine()
Else
BorderStyle = BorderStyle.Fixed3D
BackColor = Color.FromKnownColor(KnownColor.Window)
If Not IsNothing(Line) Then
Line.Visible = False
End If
End If
End Sub
Protected Overrides Sub OnParentChanged(e As EventArgs)
MyBase.OnParentChanged(e)
If Me.ShowLine Then
rePosLine()
End If
End Sub
Protected Overrides Sub OnLocationChanged(e As EventArgs)
MyBase.OnLocationChanged(e)
If Me.ShowLine Then
rePosLine()
End If
End Sub
Protected Sub rePosLine()
If Not IsNothing(Me.Parent) Then
If Not Me.Parent.Controls.Contains(Line) Then
Me.Parent.Controls.Add(Line)
End If
Line.Width = Me.Width
Line.Height = Me.LineWidth
Line.BackColor = Me.LineColor
Line.Location = New Point(Me.Location.X, Me.Location.Y + Me.Height + Me.LineOffset)
Line.Visible = True
Line.BringToFront()
End If
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
If disposing Then
If Not IsNothing(Line) Then
Line.Dispose()
End If
End If
End Sub
Protected Overrides Sub OnLayout(levent As LayoutEventArgs)
MyBase.OnLayout(levent)
If Me.ShowLine Then
rePosLine()
End If
End Sub
End Class