﻿Public Class Highlighted_textbox

    Inherits TextBox
    Public Property BorderColor As Color 'to set the color of the border
    Public Property BorderWidth As Integer ' to set the widht of the border
    Public Property Gradient As Boolean 'color gradient  for the border if true

    Sub New()
        AutoSize = False
        BorderStyle = BorderStyle.FixedSingle
        BorderColor = Color.Blue
        Gradient = False
        BorderWidth = 3
    End Sub

    Private Sub BorderPaint(Bcolor As Color, Draw As Boolean)
        'Drawing of lines around the textbox beyond the text area
        ' one set of line for each point of width
        'if gradient is true, reduce Alpha
        'to "erase" the border, draw the lines with parent backcolor
        'draw variable = false mean erase the border

        If BorderWidth = 0 Then Exit Sub 'just in case

        Dim pt1, pt2, pt3, pt4 As New Point
        For i = 1 To BorderWidth
            'create the points to draw the lines
            pt1 = New Point(Location.X - i, Location.Y - i)
            pt2 = New Point(Location.X + Width - 1 + i, Location.Y - i)
            pt3 = New Point(Location.X + Width - 1 + i, Location.Y + Height - 1 + i)
            pt4 = New Point(Location.X - i, Location.Y + Height - 1 + i)
            Dim pts As Point() = {pt1, pt2, pt3, pt4, pt1}

            'create the pen depending on gradient and draw
            Dim p As Pen
            If Gradient And Draw Then
                p = New Pen(Color.FromArgb(Bcolor.A \ (i + 1), Bcolor.R, Bcolor.G, Bcolor.B), 1)
            Else
                p = New Pen(Bcolor, 1)
            End If

            'Draw the lines in parent graphics
            Dim g As Graphics = Parent.CreateGraphics
            g.DrawLines(p, pts)
            p.Dispose()
        Next

    End Sub

    Private Sub TextBox_Mouseenter(sender As Object, e As EventArgs) Handles Me.MouseEnter
        BorderPaint(BorderColor, True)
    End Sub

    Private Sub TextBox_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
        BorderPaint(Parent.BackColor, False)
    End Sub


    Private Sub Highlighted_textbox_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus
        BorderPaint(BorderColor, True)
    End Sub

    Private Sub Highlighted_textbox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        BorderPaint(Parent.BackColor, False)
    End Sub

End Class