Custom version of Textbox in .NET

Hi friends, I have created a custom version of Textbox with some cool new properties, events….

Checkout…

 #Region "D E S I G N E R  G E N E R A T E D  C O D E"  
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class MyTextBox
Inherits System.Windows.Forms.TextBox
'UserControl1 overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
'Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
End Sub
End Class
#End Region
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox
#Region "U s e r D e f i n e d V a r i a b l e s"
Public Enum enmInputType
[Integer] = 0
[Alphabets] = 1
[Decimal] = 2
[AlphaNumeric] = 3
[EmailID] = 4
[No_Validation] = 5
End Enum
Private mInputType As enmInputType
Private mOnFocusColor As Color
Private mOnLeaveColor As Color
Private mOnFocusFont As Font
Private mOnLeaveFont As Font
Private mOnFocusSelText As Boolean
Private mNextControl As String
Private mPrevControl As String
Private mLineCount As Integer
#End Region
<System.ComponentModel.Description("Changes the BackColor on GotFocus")> _
Public Property A_OnFocusColor() As Color
Get
Return mOnFocusColor
End Get
Set(ByVal value As Color)
mOnFocusColor = value
End Set
End Property
<System.ComponentModel.Description("Changes BackColor On LostFocus")> _
Public Property A_OnLeaveColor() As Color
Get
Return mOnLeaveColor
End Get
Set(ByVal value As Color)
mOnLeaveColor = value
Me.BackColor = mOnLeaveColor
End Set
End Property
<System.ComponentModel.Description("Changes the Font on GotFocus")> _
Public Property A_OnFocusFont() As Font
Get
Return mOnFocusFont
End Get
Set(ByVal value As Font)
mOnFocusFont = value
End Set
End Property
<System.ComponentModel.Description("Changes Font On LostFocus")> _
Public Property A_OnLeaveFont() As Font
Get
Return mOnLeaveFont
End Get
Set(ByVal value As Font)
mOnLeaveFont = value
Me.Font = mOnLeaveFont
End Set
End Property
<System.ComponentModel.Description("Selects the Text On Focus if True")> _
<System.ComponentModel.DefaultValue(GetType(Boolean), "True")> _
Public Property A_OnFocusSelText() As Boolean
Get
Return mOnFocusSelText
End Get
Set(ByVal value As Boolean)
mOnFocusSelText = value
End Set
End Property
<System.ComponentModel.Description("Set the next control to which you want to set focus when user hits Enter key")> _
Public Property A_NextControl() As String
Get
Return mNextControl
End Get
Set(ByVal value As String)
mNextControl = value
End Set
End Property
<System.ComponentModel.Description("Set the control to which you want to set focus when user hits backspace key in an empty textbox")> _
Public Property A_PrevControl() As String
Get
Return mPrevControl
End Get
Set(ByVal value As String)
mPrevControl = value
End Set
End Property
<System.ComponentModel.Description("Specify which type of input you want to allow in the textbox")> _
Public Property A_InputType() As enmInputType
Get
Return mInputType
End Get
Set(ByVal value As enmInputType)
mInputType = value
End Set
End Property
<System.ComponentModel.Description("Gives the number of lines in the textbox if it is multiline")> _
Public ReadOnly Property A_LineCount() As Integer
Get
If Me.Multiline = True Then
Return mLineCount
Else
Return 0
End If
End Get
End Property
Private Sub MyTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
Me.BackColor = mOnFocusColor
Me.Font = mOnFocusFont
If mOnFocusSelText Then
Me.SelectionStart = 0
Me.SelectionLength = Len(Me.Text)
End If
End Sub
Private Sub MyTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If Me.Text.Length = 0 And e.KeyChar = Chr(8) Then
Try : Me.Parent.Controls.Item(mPrevControl).Focus() : Catch : End Try
End If
If e.KeyChar = Chr(13) Then
If Me.Multiline = False Then
Try : Me.Parent.Controls.Item(mNextControl).Focus() : Catch : End Try
e.Handled = True
End If
End If
If ValidateInput(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
Private Sub MyTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
'Changing the BackColor On Leave Event based on the property
Me.BackColor = mOnLeaveColor
Me.Font = mOnLeaveFont
If Me.Multiline = True Then
mLineCount = 0
For Each x As Char In Me.Text
If x = Chr(13) Then
mLineCount += 1
End If
Next
End If
End Sub
Private Function ValidateInput(ByRef keyC As Char) As Boolean
'Following line Accepts the Keys like Backspace, else wont accept
If Char.IsControl(keyC) Then Return True
Select Case mInputType
Case enmInputType.Integer
If Char.IsNumber(keyC) = True Or _
(keyC = "-" And Split(Me.Text, "-").Length <= 1) Then
Return True
End If
Case enmInputType.Decimal
If Char.IsNumber(keyC) = True Or _
(keyC = "." And Split(Me.Text, ".").Length <= 1) Or _
(keyC = "-" And Split(Me.Text, "-").Length <= 1) Then
Return True
End If
Case enmInputType.Alphabets
If Char.IsLetter(keyC) = True Or Char.IsWhiteSpace(keyC) Then
Return True
End If
Case enmInputType.AlphaNumeric
If Char.IsLetterOrDigit(keyC) = True Or Char.IsWhiteSpace(keyC) Then
Return True
End If
Case enmInputType.EmailID
If Char.IsLetterOrDigit(keyC) = True Or _
keyC = "_" Or _
(keyC = "." And Split(Me.Text, ".").Length >= 1) Or _
(keyC = "@" And Split(Me.Text, "@").Length <= 1) Then
Return True
End If
Case enmInputType.No_Validation
'No Validations, which is also a Default
Return True
End Select
End Function
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Following line will disable the shortcuts enabled on the control, i.e. Context Menu, Cut/Copy/Paste keys, etc..
'Me.ShortcutsEnabled = False
Me.A_InputType = enmInputType.No_Validation
Me.A_OnFocusColor = Color.White
Me.A_OnLeaveColor = Color.Silver
Me.A_OnFocusSelText = True
Me.A_OnFocusFont = System.Windows.Forms.Form.DefaultFont
Me.A_OnLeaveFont = System.Windows.Forms.Form.DefaultFont
Me.BackColor = Me.A_OnLeaveColor
Me.Font = Me.A_OnLeaveFont
End Sub
End Class
Share This!

Leave a Reply

Your email address will not be published. Required fields are marked *