Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改鼠标单击vb.net时文本框边框的颜色_Vb.net - Fatal编程技术网

更改鼠标单击vb.net时文本框边框的颜色

更改鼠标单击vb.net时文本框边框的颜色,vb.net,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 High

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

但我不知道如何实施,在哪里实施

代码如下:

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,所以在其中实现


帮助我..

假设您创建了一个Windows窗体项目,打开Form1.vb并在现有的
结束类
语句(即,在文件底部)之后通过该类(从
公共类HighlightTextBox
结束类

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

公共类Form1
行后立即复制并粘贴以下代码:

Dim t1 As New HighlightTextBox
Dim t2 As New HighlightTextBox
现在,在
专用子表单1\u Load…
结束子表单
行之间复制并粘贴以下代码

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
这将在表单中添加两个高亮文本框。当你点击一个没有焦点的时候,边框会像预期的那样变成红色。当表单打开时,如果表单上没有其他内容首先获得焦点,
t1
将默认具有焦点。然而,当窗体第一次打开时,它将不会有红色边框——不确定为什么,因为我还没有研究过这个问题,但这回答了如何实现这个类并创建它的实例的问题


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

不确定是否希望颜色跟随控件的焦点事件。目前,示例代码仅设置高光颜色,正常状态基于父级的
背景色。我修改了控件以设置这两种颜色,这取决于控件是否聚焦,我还添加了一个计时器来检查在父控件被分配后控件是否具有聚焦。这将允许它在初始加载表单时具有选定的边框颜色

看看这是否符合你的要求

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

您要在哪个控件上更改颜色?如果有人单击textbox,它将变为绿色,如果没有单击,它将变为blueHiLightTextBox\u GotFocus可能会处理HiLightTextBox.GotFocus。其中的一些“我”应该是HiLighttextBox(例如位置/大小)。你可以将我的说明与Mark Hall在另一个答案中提供的课程结合起来,以获得你在评论中提到的绿色和蓝色效果。我的理解是,您不知道将此代码放在何处,也不知道如何创建HighlightTextBox对象,所以这就是我重点关注的内容。非常感谢。。。你让我明白了。现在我知道了在哪里实现以及如何实现代码。再次感谢你,伙计:)