1

I have code for changing textbox border color on mouse click

but I could not get that how to implement it and where to implement it

here is the code:

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

I have form1 and in that there is only textbox , so in it where to implement

Help me ..

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Aditya
  • 9
  • 1
  • 7
  • on which control you want to change color ? – hdkhardik Jul 02 '15 at 17:20
  • if someone clicks on textbox , it should change to green and if there is no click ,it should change to blue – Aditya Jul 02 '15 at 17:22
  • HiLightTextBox_GotFocus should probably handle HiLightTextBox.GotFocus. And some of the "Me"s in there should be HiLighttextBox (e.g. Location/Size). – Fruitbat Jul 02 '15 at 17:27

2 Answers2

1

Assuming you have a Windows Forms project created, open Form1.vb and past this class (from Public Class HighlightTextBox to End Class) after the existing End Class statement (i.e., at the bottom of the file).

Next, you need to remove the extraneous line feed on the line that starts with Windows.Forms.ButtonBorderStyle. The previous line, as you will see, ends with an underscore, and that means that the following line is a continuation of code, so the extra line break needs to be removed so that the line will be continued.

Copy and paste the following code immediately after the Public Class Form1 line:

Dim t1 As New HighlightTextBox
Dim t2 As New HighlightTextBox

Now, copy and paste the following code between the Private Sub Form1_Load... and End Sub lines.

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

This will add two HighlightTextBoxes to the form. When you click on the one that doesn't have focus, the border will turn red as expected. When the form opens, if there is nothing else on the form that gets focus first, t1 will have focus by default. However, it will not have a red border when the form first opens--not sure why as I haven't worked on that yet, but this answers the question of how to implement this class and create instances of it.

Also see How To Put a Usercontrol into Visual Studio Toolbox. I'm using VS 2013 and didn't need to make this change, but once you unload/reload the project, the HighlightTextBox will show up in the Toolbox so you can easily add them in the designer.

Community
  • 1
  • 1
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
  • 1
    You can combine my instructions with the class that Mark Hall provided in the other answer to get the green and blue effects you mentioned in the comments. My understanding was that you didn't know where to put this code or how to create the HighlightTextBox objects, so that's what I focused on. – Tony Hinkle Jul 02 '15 at 19:43
  • Thanks a lot ... u made a me all clear. Now i got where to implement and how to implement the code . Again thanks buddy :) – Aditya Jul 03 '15 at 05:42
1

Not sure if you are wanting to have the color follow the focus event of the Control or not. As it stands your example code is only setting the Highlight Color, the normal state is based on the Parent's BackColor. I modified your control to set both colors and it is based on whether or not the control is focused, I also added a timer to check whether or not the control has focus after the parent has be assigned this will allow it to have the selected border color upon initial load of the Form.

See if this does what you are wanting.

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
Mark Hall
  • 53,938
  • 9
  • 94
  • 111