【问题标题】:change textbox border color on mouse click vb.net在鼠标单击 vb.net 时更改文本框边框颜色
【发布时间】:2015-09-20 08:27:46
【问题描述】:

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

但我不知道如何实现它以及在哪里实现它

代码如下:

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 并且只有 textbox ,所以在哪里实现

帮帮我..

【问题讨论】:

  • 你想在哪个控件上改变颜色?
  • 如果有人点击文本框,它应该变成绿色,如果没有点击,它应该变成蓝色
  • HiLightTextBox_GotFocus 应该可以处理 HiLightTextBox.GotFocus。并且其中的一些“我”应该是 HiLighttextBox(例如位置/大小)。

标签: vb.net


【解决方案1】:

假设您创建了一个 Windows 窗体项目,打开 Form1.vb 并在现有的 End Class 语句之后(即文件底部)过去这个类(从 Public Class HighlightTextBoxEnd 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 将显示在工具箱中,因此您可以轻松地将它们添加到设计器中。

【讨论】:

  • 您可以将我的说明与 Mark Hall 在另一个答案中提供的课程相结合,以获得您在 cmets 中提到的绿色和蓝色效果。我的理解是你不知道把这段代码放在哪里或者如何创建 HighlightTextBox 对象,所以这就是我关注的。
  • 非常感谢......你让我明白了。现在我知道了在哪里实现以及如何实现代码。再次感谢哥们:)
【解决方案2】:

不确定您是否想让颜色跟随控件的焦点事件。就目前而言,您的示例代码仅设置突出显示颜色,正常状态基于父级的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2019-08-12
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多