Introduction
This extension allows you to add validation functionality to a textbox
without having to re-write on the OnKeyPress
commands over and over again. Also, it captures the WndProc
for pasting into the textbox
and makes sure that the pasted text matches that of the current validation mode.
You have 8 new properties that cover Regular Expression and Empty Field properties (Empty string)
Regex_LiveExpression
- This property is readonly, it displays the current Regular Expression being used. Regex_Mode
- Select the mode you want to use, Any, Numbers Only, AlphaNumeric, Custom
NumbersOnly
- Uses [0-9] AlphaNumeric -
Uses [0-9a-zA-Z] Custom -
Uses the Custom Expression you enter into the Regex_CustomRegEx Property
Regex_CustomRegEx -
Enter your regular expression here (works in Conjuction with Regex_Mode)
Regex_AllowPaste
-
Uses the Custom Expression you enter into the Regex_CustomRegEx
Property Regex_AllowPeriods -
Use this to supercede all regular expression for (dot) period entry, usually used with NumbersOnly Regex_Mode
to allow double
types (111.222) AllowBlank
- This property determines if to allow the field to become empty AllowBlankDefaultValue
- if the property becomes empty and AllowBlank is false, the value will be entered into the textbox to avoid empty fields, save messy text validations for large number of numeri/currency fields so you could enter 0.00 as your default value, then... no need to code loads of validations as it is already done for you. AllowBlankWarningMessage
- This property, if not blank, will set a messagebox to the user with what ever text is here. If the property is left blank it will not display a message.
The great thing about this is that you can set the regular expression by textbox
without having to rebuild a new class through the properties so you could set to 'Text Only' and enter the regular expression for say 'Email' address, etc. I built this because I hate having to rewrite keypress
events.
Using the Code
On your project, add a new class and add this code into the new class, then rebuild your project/solution.
You will now see RegExTextbox
appear in the top right of your toolbox.
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.ComponentModel
<ToolboxBitmap(GetType(RegExTextbox), "RegEx.bmp")> Partial Public Class RegExTextbox
Inherits TextBox
#Region "Regex Modes Properties"
Public Enum RegexMode
None = 0
NumbersOnly = 1
AlphaNumeric = 2
Custom = 3
End Enum
Private _prvRegex_Mode As RegexMode = RegexMode.None
Private _prvRegex_AllowPeriod As Boolean = True
Private _prvRegex_AllowPeriodRegex As String = "|[.]"
Private _prvRegex_NumbersOnly As String = "[0-9]"
Private _prvRegex_AlphaNumeric As String = "[0-9a-zA-Z]"
Private _prvRegex_CustomRegex As String = ""
Private _prvRegex_CTRLKeyDown As Boolean = False
Private _prvRegex_AllowPaste As Boolean = True
<Description("Determine which Regular Expression Mode to use")> _
Public Property Regex_Mode As RegexMode
Get
Return _prvRegex_Mode
End Get
Set(value As RegexMode)
_prvRegex_Mode = value
End Set
End Property
<Description("The current expression that is being used by the control")> _
Public ReadOnly Property Regex_LiveExpression As String
Get
Dim tmpReturn As String
Select Case _prvRegex_Mode
Case RegexMode.NumbersOnly : tmpReturn = _prvRegex_NumbersOnly
Case RegexMode.AlphaNumeric : tmpReturn = _prvRegex_AlphaNumeric
Case RegexMode.Custom : tmpReturn = _prvRegex_CustomRegex
Case Else : tmpReturn = ""
End Select
Return tmpReturn & IIf(String.IsNullOrEmpty(tmpReturn), String.Empty, IIf(Regex_AllowPeriods = True, _prvRegex_AllowPeriodRegex, String.Empty))
End Get
End Property
<Description("If Regex_Mode is Custom, the control will use the regular expression entered here.")> _
Public Property Regex_CustomRegEx As String
Get
Return _prvRegex_CustomRegex
End Get
Set(value As String)
_prvRegex_CustomRegex = value
End Set
End Property
<Description("A boolean to determine if 'Periods' (dot) are allowed - this will overwrite in custom mode")> _
Public Property Regex_AllowPeriods As Boolean
Get
Return _prvRegex_AllowPeriod
End Get
Set(value As Boolean)
_prvRegex_AllowPeriod = value
End Set
End Property
<Description("Allow Pasting into the Textbox, When a user pastes into the field it will use the current RegexMode")> _
Public Property Regex_AllowPaste As Boolean
Get
Return _prvRegex_AllowPaste
End Get
Set(value As Boolean)
_prvRegex_AllowPaste = value
End Set
End Property
#End Region
#Region "Blank Textbox Properties"
Private _prvBlankAllow As Boolean = True
Private _prvBlankDefaultVal As String = "0"
Private _prvBlankWarningMessage As String = ""
<Description("Determines if the field is allowed to be blank, See also AllowBlankDefaultValue/AllowBlankWarningMessage ")> _
Public Property AllowBlank As Boolean
Get
Return _prvBlankAllow
End Get
Set(value As Boolean)
_prvBlankAllow = value
End Set
End Property
<Description("The default value to replace with if the control is empty")> _
Public Property AllowBlankDefaultValue As String
Get
Return _prvBlankDefaultVal
End Get
Set(value As String)
If (Len(value) = 0) Then
MsgBox("Allow Blank default value cannot be null, setting to 0", vbCritical + vbOKOnly, "Property Cannot be Null")
_prvBlankDefaultVal = "0"
Else
_prvBlankDefaultVal = value
End If
End Set
End Property
<Description("The message to be displayed if ther user deletes all the text [Optional] Leave blank for no message")> _
Public Property AllowBlankWarningMessage As String
Get
Return _prvBlankWarningMessage
End Get
Set(value As String)
_prvBlankWarningMessage = value
End Set
End Property
#End Region
Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
If (e.Control) Then _prvRegex_CTRLKeyDown = True
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnKeyUp(e As System.Windows.Forms.KeyEventArgs)
If (e.Control) Then _prvRegex_CTRLKeyDown = False
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
If (_prvRegex_Mode <> RegexMode.None) Then
If (_prvRegex_CTRLKeyDown = True) Then
Else
If ((Regex.Match(e.KeyChar, Regex_LiveExpression).Captures.Count = 0) And (Not e.KeyChar = Chr(8))) Then
e.Handled = True
End If
End If
End If
MyBase.OnKeyPress(e)
End Sub
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
If (AllowBlank = False) Then
If (Me.TextLength = 0) Then Me.Text = AllowBlankDefaultValue
If (Len(AllowBlankWarningMessage) > 0) Then
MsgBox(AllowBlankWarningMessage, vbOKOnly, "Text Validation Error")
End If
End If
MyBase.OnTextChanged(e)
End Sub
#Region "Paste Handling"
Private Const WM_PASTE As Integer = &H302
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PASTE AndAlso _prvRegex_AllowPaste = True AndAlso Clipboard.ContainsText Then
If (_prvRegex_Mode <> RegexMode.None) Then
Dim str_Clipboard As String = ""
For Each regMatch As Match In Regex.Matches(Clipboard.GetText, Regex_LiveExpression)
str_Clipboard &= regMatch.Value
Next
If Not (String.IsNullOrEmpty(str_Clipboard)) Then
Dim tmpCBD As String = Clipboard.GetText
Clipboard.SetText(str_Clipboard)
MyBase.WndProc(m)
Else
End If
End If
Else
MyBase.WndProc(m)
End If
End Sub
#End Region
End Class
Points of Interest
Nothing to report but I will be happy to get any comments / suggestions.
History
- 15th December, 2014: Initial upload
- 16th December, 2014: Added functionality for Null Value handling
- Added functionality for null value handling (set to default value if string length is 0)
- Improved code
- Better Commentry
- Descriptions for property boxes
- Improve handling of WndProc for Paste
- Better RegEx handling on Paste