Extended TextBox Component Class - VB.Net


How to Create a reusable TextBox Component Class in VB.Net [ Class : KTextBox ]

How to use this code..?



1) Create a new project Visual Basic -Windows Application..

2) From Project menu add a class or component class

3) Copy and paste the following code to code page

4) Now you can drag and drop the KTextBox control to your form from the toolbox...

5) Try it…





Imports System.ComponentModel
Imports System.Windows.Forms
<System.ComponentModel.DefaultEvent("OnVaidationError")> _
Public Class KTextBox
Inherits System.Windows.Forms.TextBox
#Region " Enumerators [ KTextBox ]"
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Public Enum KTB_INPUTTYPES_SETTINGS
AlphaNumeric = 0
AlphabetsOnly = 1
Currency = 2
Custom = 3
DecimalNumeric = 4
EmailAddress = 5
IntegerNumeric = 6
Normal = 7
PhoneNumber = 8
SerialNumber = 9
End Enum
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Public Enum KTB_AUTOFORMAT_SETTINGS
None = 0
LowerCase = 1
UpperCase = 2
TitleCase = 3
End Enum
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Public Enum KTB_PRECISION_SETTINGS
None = 0
OneDecimal = 1
TwoDecimal = 2
ThreeDecimal = 3
End Enum
#End Region
#Region " Declarations [ KTextBox ]"
Private _mInputType As KTB_INPUTTYPES_SETTINGS = KTB_INPUTTYPES_SETTINGS.Normal
Private _mPrecision As KTB_PRECISION_SETTINGS = KTB_PRECISION_SETTINGS.None
Private _mAutoFormatStyle As KTB_AUTOFORMAT_SETTINGS = KTB_AUTOFORMAT_SETTINGS.None
Private _mEnterFocusColor As Color
Private _mLeaveFocusColor As Color
Private _mCustomTypeString As String
Private _mMandatoryField As Boolean = False
Private _mMandatoryColor As Color
Private _mRegularExpression As String
Private _mRegularExpressionErrMsg As String
Private _mShowMsg As Boolean = True
Private _mIsValidated As Boolean = False
#End Region
#Region " Events Declarations [ KTextBox ]"
Public Event OnVaidationError(ByVal _ErrorMsg As String)
#End Region
#Region " Properties [ KTextBox ]"
''' <summary>Set or Gets TextBox Input Types as KTB_INPUTTYPES_SETTINGS</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Gets TextBox Types as KTB_INPUTTYPES_SETTINGS")> _
Public Property InputType() As KTB_INPUTTYPES_SETTINGS
Get
Return _mInputType
End Get
Set(ByVal value As KTB_INPUTTYPES_SETTINGS)
_mInputType = value
End Set
End Property
''' <summary>Set or Get the color of the control when it receives focus</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get the color of the control when it receives focus")> _
Public Property EnterFocusColor() As Color
Get
Return _mEnterFocusColor
End Get
Set(ByVal Value As Color)
_mEnterFocusColor = Value
End Set
End Property
''' <summary>Set or Get the color of the control when it lost focus</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get the color of the control when it lost focus")> _
Public Property LeaveFocusColor() As Color
Get
Return _mLeaveFocusColor
End Get
Set(ByVal Value As Color)
_mLeaveFocusColor = Value
End Set
End Property
''' <summary>Set or Get custom control's Custom Input Type string format</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get custom control's Custom Input Type string format")> _
Public Property CustomInputTypeString() As String
Get
Return _mCustomTypeString
End Get
Set(ByVal Value As String)
_mCustomTypeString = Value
End Set
End Property
''' <summary>Set or Get control Auto Format Style</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get control's Auto Format Style as KTB_AUTOFORMAT_SETTINGS")> _
Property AutoFormat() As KTB_AUTOFORMAT_SETTINGS
Get
Return _mAutoFormatStyle
End Get
Set(ByVal Value As KTB_AUTOFORMAT_SETTINGS)
_mAutoFormatStyle = Value
End Set
End Property
''' <summary>Set or Get controls mandatory status</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get controls mandatory status")> _
Public Property MandatoryField() As Boolean
Get
Return _mMandatoryField
End Get
Set(ByVal Value As Boolean)
_mMandatoryField = Value
End Set
End Property
''' <summary>Set or Get the color of the control when the mandatory field is empty</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get the color of the control when the mandatory field is empty")> _
Public Property MandatoryColor() As Color
Get
Return _mMandatoryColor
End Get
Set(ByVal Value As Color)
_mMandatoryColor = Value
End Set
End Property
''' <summary>Set or Get Number of decimal places</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get Number of decimal places")> _
Public Property Precision() As KTB_PRECISION_SETTINGS
Get
Return _mPrecision
End Get
Set(ByVal value As KTB_PRECISION_SETTINGS)
_mPrecision = value
End Set
End Property
''' <summary>Set or Get Regular Expression for Custom Validation</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get Regular Expression for Custom Validation")> _
Public Property RegularExpression() As String
Get
Return _mRegularExpression
End Get
Set(ByVal value As String)
_mRegularExpression = value
End Set
End Property
''' <summary>Set or Get Regular Expression Validation Custom Error Message</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get Regular Expression Validation Custom Error Message")> _
Public Property RegularExpressionErrorMessage() As String
Get
Return _mRegularExpressionErrMsg
End Get
Set(ByVal value As String)
_mRegularExpressionErrMsg = value
End Set
End Property
''' <summary>Set or Get automatic validation error display status</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get automatic validation error display status")> _
Public Property ShowMessage() As Boolean
Get
Return _mShowMsg
End Get
Set(ByVal Value As Boolean)
_mShowMsg = Value
End Set
End Property
''' <summary>Set or Get controls validation status</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Set or Get controls validation status")> _
Public Property IsValidated() As Boolean
Get
Return _mIsValidated
End Get
Set(ByVal Value As Boolean)
_mIsValidated = Value
End Set
End Property
#End Region
#Region " Methods [ KTextBox ]"
''' <summary>Calls to Show Controls Messages..</summary>
<System.ComponentModel.Category("Features")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<System.ComponentModel.Description("Calls to Show Controls Messages..")> _
Public Sub PopupBalloon(ByVal _Message As String, Optional ByVal _TipIcon As ToolTipIcon = ToolTipIcon.Info, Optional ByVal _TipTitle As String = "Input Validation Error..!!")
Dim _ToolTip As New ToolTip
_ToolTip.IsBalloon = True
_ToolTip.ToolTipTitle = _TipTitle
_ToolTip.ToolTipIcon = _TipIcon
_ToolTip.UseAnimation = True
_ToolTip.UseFading = True
_ToolTip.Show(_Message, Me, 20, -62, 2000)
End Sub
#End Region
#Region " Control Events [ KTextBox ]"
Private Sub OnTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
Me.BackColor = Me._mEnterFocusColor
End Sub
Private Sub OnKTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
If ((Me.MandatoryField = True) And (CType(sender, TextBox).Text = String.Empty)) Then
Me.BackColor = Me._mMandatoryColor
_mIsValidated = False
Else
Me.BackColor = Me._mLeaveFocusColor
_mIsValidated = True
End If

If ((IsNumeric(CType(sender, TextBox).Text) = True) And ((_mInputType = KTB_INPUTTYPES_SETTINGS.Currency) Or (_mInputType = KTB_INPUTTYPES_SETTINGS.DecimalNumeric))) Then
Select Case _mPrecision
Case KTB_PRECISION_SETTINGS.OneDecimal
CType(sender, TextBox).Text = Microsoft.VisualBasic.FormatNumber(CType(sender, TextBox).Text, 1, , , TriState.False)
Case KTB_PRECISION_SETTINGS.TwoDecimal
CType(sender, TextBox).Text = Microsoft.VisualBasic.FormatNumber(CType(sender, TextBox).Text, 2, , , TriState.False)
Case KTB_PRECISION_SETTINGS.ThreeDecimal
CType(sender, TextBox).Text = Microsoft.VisualBasic.FormatNumber(CType(sender, TextBox).Text, 3, , , TriState.False)
Case KTB_PRECISION_SETTINGS.None
CType(sender, TextBox).Text = Microsoft.VisualBasic.FormatNumber(CType(sender, TextBox).Text, 0, , , TriState.False)
End Select
End If

Select Case _mInputType
Case KTB_INPUTTYPES_SETTINGS.AlphabetsOnly, KTB_INPUTTYPES_SETTINGS.AlphaNumeric, KTB_INPUTTYPES_SETTINGS.Custom, KTB_INPUTTYPES_SETTINGS.Normal, KTB_INPUTTYPES_SETTINGS.SerialNumber
Select Case _mAutoFormatStyle
Case KTB_AUTOFORMAT_SETTINGS.UpperCase
CType(sender, TextBox).Text = CType(sender, TextBox).Text.ToUpper
Case KTB_AUTOFORMAT_SETTINGS.LowerCase
CType(sender, TextBox).Text = CType(sender, TextBox).Text.ToLower
Case KTB_AUTOFORMAT_SETTINGS.TitleCase
CType(sender, TextBox).Text = Microsoft.VisualBasic.StrConv(CType(sender, TextBox).Text, VbStrConv.ProperCase)
Case KTB_AUTOFORMAT_SETTINGS.None
'DO NOTHING
End Select
Case Else
'DO NOTHING
End Select

Dim _TxtBox As TextBox = CType(sender, TextBox)
Dim _RegularExp As System.Text.RegularExpressions.Regex

If _mRegularExpression <> String.Empty Then
_RegularExp = New System.Text.RegularExpressions.Regex(_mRegularExpression)
If Not _RegularExp.IsMatch(_TxtBox.Text) Then
If (_mShowMsg = True) Then Call PopupBalloon(_mRegularExpressionErrMsg, ToolTipIcon.Error)
RaiseEvent OnVaidationError(_mRegularExpressionErrMsg)
_mIsValidated = False
Exit Sub
Else
_mIsValidated = True
End If
End If

Select Case _mInputType
Case KTB_INPUTTYPES_SETTINGS.IntegerNumeric
_RegularExp = New System.Text.RegularExpressions.Regex("^-[0-9]+$|^[0-9]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Integers Values Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Integers Values Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.DecimalNumeric
If Not (IsNumeric(_TxtBox.Text)) Then
If (_mShowMsg = True) Then Call PopupBalloon("Numeric Values Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Numeric Values Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.AlphabetsOnly
_RegularExp = New System.Text.RegularExpressions.Regex("^[a-zA-Z]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Alphabets Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Alphabets Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.AlphaNumeric
_RegularExp = New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 ]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("AlphaNumeric Values Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("AlphaNumeric Values Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.Currency
_RegularExp = New System.Text.RegularExpressions.Regex("^-[0-9\.]+$|^[0-9\.]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Currency Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Currency Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.PhoneNumber
_RegularExp = New System.Text.RegularExpressions.Regex("^[0-9()+\-]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Phone Number Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Phone Number Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.SerialNumber
_RegularExp = New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 \-]+$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Serial Number Expected..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Serial Number Expected..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.EmailAddress
_RegularExp = New System.Text.RegularExpressions.Regex("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
If (_mShowMsg = True) Then Call PopupBalloon("Invalid Email Address..!!", ToolTipIcon.Error)
RaiseEvent OnVaidationError("Invalid Email Address..!!")
_mIsValidated = False
Else
_mIsValidated = True
End If
Case KTB_INPUTTYPES_SETTINGS.Custom, KTB_INPUTTYPES_SETTINGS.Normal
If (CType(sender, TextBox).Text = String.Empty) Then
_mIsValidated = False
Else
_mIsValidated = True
End If
End Select
End Sub
Private Sub OnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Select Case _mInputType
Case KTB_INPUTTYPES_SETTINGS.IntegerNumeric
If ((Char.IsDigit(e.KeyChar) = False) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.DecimalNumeric
If ((Char.IsDigit(e.KeyChar) = False) And (Char.IsControl(e.KeyChar) <> True) And (e.KeyChar <> "."c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("."c) = True) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text = String.Empty) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.AlphabetsOnly
If ((Char.IsLetter(e.KeyChar) = False) And ((e.KeyChar <> Chr(32))) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.AlphaNumeric
If ((Char.IsLetterOrDigit(e.KeyChar) = False) And ((e.KeyChar <> Chr(32))) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.Currency
If ((Char.IsDigit(e.KeyChar) = False) And (e.KeyChar <> "."c) And (e.KeyChar <> "+"c) And (e.KeyChar <> "-"c) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("."c) = True) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("-"c) = True) And (e.KeyChar = "-"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("+"c) = True) And (e.KeyChar = "+"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text = String.Empty) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
If ((e.KeyChar = "."c) And (CType(sender, TextBox).Text.Length = 1) And ((CType(sender, TextBox).Text = "+") Or (CType(sender, TextBox).Text = "-"))) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text <> String.Empty) And (e.KeyChar = "+"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text <> String.Empty) And (e.KeyChar = "-"c)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.PhoneNumber
If ((Char.IsDigit(e.KeyChar) = False) And (e.KeyChar <> "-"c) And (e.KeyChar <> "("c) And (e.KeyChar <> ")"c) And (e.KeyChar <> "+"c) And (Char.IsWhiteSpace(e.KeyChar) = False) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("("c) = True) And (e.KeyChar = "("c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains(")"c) = True) And (e.KeyChar = ")"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("+"c) = True) And (e.KeyChar = "+"c)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.Normal
'DO NOTHING
Case KTB_INPUTTYPES_SETTINGS.EmailAddress
If ((CType(sender, TextBox).Text.Contains("@"c) = True) And (e.KeyChar = "@"c)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.SerialNumber
If ((Char.IsLetterOrDigit(e.KeyChar) = False) And (e.KeyChar <> "-"c) And (Char.IsWhiteSpace(e.KeyChar) = False) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case KTB_INPUTTYPES_SETTINGS.Custom
If ((InStr(Me._mCustomTypeString, e.KeyChar.ToString, CompareMethod.Binary) = 0) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
End Select
End Sub
#End Region
End Class




Comments

Guest Author: joseph naoe12 Nov 2012

how do you use this ?

Guest Author: Allan16 Feb 2013

is there any callbacks to the ktextbox so that I'll be able to check if the textbox passed the validation after clicking for example a command button?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: