I'm very new to Visual Studio and I'm wanting to create my own TextBox that can have customizable borders (and maybe other stuff in future).
Right now, my method is merging a couple things together to sort of Frankenstein my own creation.
I've created a Class, built it, browsed and added it to the Toolbox in my Form (another project) and placed it in the Form. All the controls show up in the properties and the TextBox loads when I build and open the Form (no errors) but of course, it just shows up as a regular TextBox (no colored border).
I can't figure out why it's not working. Is it the rectangle that I create the problem, or some sort of painting problem, something to do with Usercontrols or something else that I'm missing?
Code:
Imports System.Windows.Forms
Imports System.Drawing
Public Class CustomTextBox
Inherits System.Windows.Forms.TextBox
Public Enum BorderSideOptions
Left
Right
Top
Bottom
All
End Enum
Dim BrdrColor As Color = Color.Blue
Dim BrdrSize As Single = 1
Dim BrdrStyle As ButtonBorderStyle = ButtonBorderStyle.Solid
Dim BorderSide As BorderSideOptions = BorderSideOptions.All
Public Sub New()
Me.Width = 120
Me.Height = 20
Me.BackColor = Color.White
Me.ForeColor = Color.Black
End Sub
Property BorderSides As BorderSideOptions
Get
Return BorderSide
End Get
Set(value As BorderSideOptions)
BorderSide = value
End Set
End Property
Property BorderColor() As Color
Get
Return BrdrColor
End Get
Set(value As Color)
BrdrColor = value
End Set
End Property
Property BorderSize() As Single
Get
Return BrdrSize
End Get
Set(value As Single)
BrdrSize = value
End Set
End Property
Overloads Property BorderStyle() As ButtonBorderStyle
Get
Return BrdrStyle
End Get
Set(value As ButtonBorderStyle)
BrdrStyle = value
End Set
End Property
Private Sub CustomTextBox_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim txtRect As Rectangle, B As Color = Color.Black
txtRect = New Rectangle(Location, Size)
Select Case BorderSides
Case BorderSideOptions.All
ControlPaint.DrawBorder(e.Graphics, txtRect, BrdrColor, BrdrSize, BrdrStyle, BrdrColor, BrdrSize, BrdrStyle, BrdrColor, BrdrSize, BrdrStyle, BrdrColor, BrdrSize, BrdrStyle)
Case BorderSideOptions.Bottom
ControlPaint.DrawBorder(e.Graphics, txtRect, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None, BrdrColor, BrdrSize, BrdrStyle)
Case BorderSideOptions.Left
ControlPaint.DrawBorder(e.Graphics, txtRect, BrdrColor, BrdrSize, BrdrStyle, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None)
Case BorderSideOptions.Right
ControlPaint.DrawBorder(e.Graphics, txtRect, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None, BrdrColor, BrdrSize, BrdrStyle, B, 0, ButtonBorderStyle.None)
Case BorderSideOptions.Top
ControlPaint.DrawBorder(e.Graphics, txtRect, B, 0, ButtonBorderStyle.None, BrdrColor, BrdrSize, BrdrStyle, B, 0, ButtonBorderStyle.None, B, 0, ButtonBorderStyle.None)
End Select
End Sub
End Class