Convert any textbox to numbers only.On many occasions in the forums this is often asked, so here is a solution that makes the process simpler.
Define a new public class in your project called
NumberOnlyTextboxCODE
Public Class NumberOnlyTextbox
Inherits TextBox
Protected m_ctrl As TextBox
Protected m_throw As KeyEventHandler
Protected m_AllowMinus As Boolean
Protected m_AllowDecimalsPoint As Boolean
To allow this capability to happen we need to supply a new instance of this class with a few details;-
The Textbox Control
Are decimal points allowed?
Are minus allowed?
And the AddressOf of a subroutine to handle to allowed keypressed. (Using Nothing if you don't provide one)
CODE
Public Sub New(ByRef ctrl As TextBox, ByRef AllowDecimalsPoint As Boolean, ByRef AllowMinus As Boolean, ByRef throwb As KeyEventHandler)
' Pass a reference to the textbox
m_ctrl = ctrl
' Are Decimal Points allowed through
m_AllowDecimalsPoint = AllowDecimalsPoint
' Are minus allowed through
m_AllowMinus = AllowMinus
' Pass a reference to the delegate. (This is AddressOf a sub which further process the keypress.)
m_throw = throwb
' Add an handler for the keydown event
AddHandler m_ctrl.KeyDown, AddressOf M_OnKeyDown
End Sub
The next sub handles the keydown event, filtering them. Passing them on the supplied sub.
CODE
Protected Sub M_OnKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
e.SuppressKeyPress = False
Select Case e.KeyCode
' Allow Digit keys through.
Case Keys.Decimal, Keys.OemPeriod
If m_AllowDecimalsPoint Then
If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
End If
Case Keys.Subtract, Keys.OemMinus
If m_AllowMinus Then
If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
End If
Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9
If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
Case Keys.Delete, Keys.Back, Keys.Enter, Keys.Return, Keys.Left, Keys.Right, Keys.Tab
' Allow "STANDARD" editting keys
If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
Case Else
' Supress all other key.
e.Handled = True
e.SuppressKeyPress = True
End Select
End Sub
The following routine removes the event handler previously defined during initialization.
CODE
Protected Overrides Sub Finalize()
' Remember to remove the handle
RemoveHandler m_ctrl.KeyDown, AddressOf M_OnKeyDown
MyBase.Finalize()
End Sub
End Class
Using the classPlace a textbox on to a form,
Form1 &
TextBox1 in this example.
Then add the following code to the form's code.
CODE
Public Class Form1
Dim a As NumberOnlyTextbox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a = New NumberOnlyTextbox(Me.TextBox1, True, True, AddressOf M_OnKeyDown)
End Sub
Private Sub M_OnKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Console.WriteLine(e.KeyCode)
End Sub
End Class
Run. See what you can enter in textbox.
Note: That it is still possible to create malformed numbers but detecting this is left as a requirement for the developer.