在鼠标单击vb.net上更改文本框边框颜色

时间:2015-07-02 17:19:02

标签: vb.net

我有用于在鼠标点击时更改文本框边框颜色的代码

但是我无法理解如何实现它以及在哪里实现它

这是代码:

using  controlpaint.DrawBorder ,you can draw with penwidth greater than 1

Public Class HighlightTextBox
    Inherits System.Windows.Forms.TextBox

    'Default Highlight color is red.>>
    Private Highlight_Color As Color = Color.Red

    Public Property HighLightColor() As Color
        Get
            Return Me.Highlight_Color
        End Get
        Set(ByVal value As Color)
            Me.Highlight_Color = value
        End Set
    End Property

    Private Pen_Width As Integer = 1

    Public Property PenWidth() As Integer
        Get
            Return Me.Pen_Width
        End Get
        Set(ByVal value As Integer)
            Me.Pen_Width = value
        End Set
    End Property

    Private Sub HiLightTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus

        Dim g As Graphics = Me.Parent.CreateGraphics

        Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2)

        Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, _

        Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid)
    End Sub

    Private Sub HiLightTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus

        Dim g As Graphics = Me.Parent.CreateGraphics
        Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width + Me.Pen_Width, Me.Width, Me.Height + Me.Pen_Width)
        g.DrawRectangle(New Pen(Parent.BackColor, Me.Pen_Width), Rect)
        Parent.Refresh()

    End Sub

    Private Sub HiLightTextBox_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        Me.Refresh()
        Call HiLightTextBox_GotFocus(Nothing, Nothing)

    End Sub
End Class

我有form1,因为只有文本框,所以在其中实现

帮帮我......

2 个答案:

答案 0 :(得分:1)

假设您已创建Windows窗体项目,请在现有Public Class HighlightTextBox语句后(即位于底部,打开Form1.vb并通过此类(从End ClassEnd Class)文件)。

接下来,您需要删除以Windows.Forms.ButtonBorderStyle开头的行上的无关换行符。正如您将看到的,上一行以下划线结尾,这意味着以下行是代码的延续,因此需要删除额外的换行符以便继续行。

Public Class Form1行后立即复制并粘贴以下代码:

Dim t1 As New HighlightTextBox
Dim t2 As New HighlightTextBox

现在,在Private Sub Form1_Load...End Sub行之间复制并粘贴以下代码。

t1.Name = "MyHTB1"
Me.Controls.Add(t1)
t1.Top = 20
t1.Left = 20

t2.Name = "MyHTB2"
Me.Controls.Add(t2)
t2.Top = 50
t2.Left = 20

这将向表单添加两个HighlightTextBox。当您单击没有焦点的那个时,边框将按预期变为红色。当表单打开时,如果表单上没有任何其他内容可以首先获得焦点,则默认情况下t1将具有焦点。但是,当表单首次打开时,它不会有红色边框 - 不知道为什么我还没有处理过,但是这回答了如何实现这个类并创建它的实例的问题。

另见How To Put a Usercontrol into Visual Studio Toolbox。我正在使用VS 2013并且不需要进行此更改,但是一旦卸载/重新加载项目,HighlightTextBox将显示在工具箱中,以便您可以轻松地将它们添加到设计器中。

答案 1 :(得分:1)

不确定您是否希望颜色跟随Control的焦点事件。因为它代表您的示例代码仅设置突出显示颜色,正常状态基于父BackColor。我修改你的控件以设置两种颜色,它是基于控件是否聚焦,我还添加了一个计时器,以检查控件是否具有焦点后,父级被分配,这将允许它具有选定的边框表格初始加载时的颜色。

看看这是否符合您的要求。

Public Class HighlightTextBox
    Inherits System.Windows.Forms.TextBox

    'Default Highlight color is red.>>
    Private Highlight_Color As Color = Color.Green
    Private Normal_Color As Color = Color.Blue
    Private WithEvents tmr As Timer = New Timer

    Public Property HighLightColor() As Color
        Get
            Return Me.Highlight_Color
        End Get
        Set(ByVal value As Color)
            Me.Highlight_Color = value
        End Set
    End Property
    Private Property NormalColor() As Color
        Get
            Return Normal_Color
        End Get
        Set(value As Color)
            Normal_Color = value
        End Set
    End Property

    Private Pen_Width As Integer = 1

    Public Property PenWidth() As Integer
        Get
            Return Me.Pen_Width
        End Get
        Set(ByVal value As Integer)
            Me.Pen_Width = value
        End Set
    End Property


    Private Sub SetHiLight()
        Dim g As Graphics = Me.Parent.CreateGraphics
        Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2)
        Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid)
    End Sub
    Private Sub SetNormal()
        Dim g As Graphics = Me.Parent.CreateGraphics
        Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2)
        Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid)
    End Sub

    Private Sub HiLightTextBox_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        Me.Refresh()
        Call SetHiLight()

    End Sub

    Protected Overrides Sub OnParentChanged(e As EventArgs)
        MyBase.OnParentChanged(e)
        tmr.Start()
    End Sub

    Protected Overrides Sub OnGotFocus(e As EventArgs)
        MyBase.OnGotFocus(e)
        SetHiLight()
    End Sub
    Protected Overrides Sub OnLostFocus(e As EventArgs)
        MyBase.OnLostFocus(e)
        SetNormal()
    End Sub

    Public Sub New()
        tmr.Interval = 10
        AddHandler tmr.Tick, AddressOf Delay
    End Sub
    Private Sub Delay(sender As Object, e As EventArgs)
        tmr.Stop()
        If Me.Focused = True Then
            SetHiLight()
        Else
            SetNormal()
        End If

    End Sub

End Class