I've been working on a Textbox that only accepts 0-9, Decimal Point, and the Backspace Keys. Also keeps it in the base form 0.00. Max value is 999999.99 can be set as needed. It works as expected, but I have to ask if I have remade the wheel. This sub is set as the KeyPress Event for the textboxes on the form and will not allow wrong KeyChar's to be entered. Any tips or criticisms welcome..
''' <summary>
''' Previews the Key stroke attempting to be entered into textbox
''' allows or disallows the key depending on the
''' e.KeyChar, position, and Caret Position.
''' Only 0-9 a single decimal point and 2 places after the decimal.
''' The Backspace key is also allowed
''' Max Value = 999999.99
''' </summary>
''' <param name="sender">TextBox</param>
''' <param name="e"> Handled = true disallows Key :: Handled = False Allows Key
''' </param>
''' <remarks></remarks>
Public Sub BoxKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
'allow Backspace and exit the sub no need to process further
If e.KeyChar = Chr(8) Then e.Handled = False : Exit Sub
Dim tb As TextBox = DirectCast(sender, TextBox) 'Get the TextBox.
'Fill Physical variables
'Decimal Point(if present) Location :: If not present (-1)
Dim point As Integer = tb.Text.IndexOf(".")
Dim caretIndex As Integer = tb.SelectionStart 'Caret position
Dim len As Integer = tb.Text.Length 'Length of text present already in textbox
'Check if a digit has been entered
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
'If the Key being entered is a Decimal Point and it will
'be the First Character then allow it and exit the Sub.
If e.KeyChar = "." And point = -1 Then e.Handled = False
'if it wasn't handled above check further
If Not e.Handled Then
If point = -1 Then 'No Decimal Point has been entered : Allow digits
e.Handled = False
ElseIf (len - point) > 2 Then '2 Decimal Places are present : Disallow Digits
e.Handled = True
End If
'if user moves caret to add keychar to within string
If caretIndex < len Then
If point > -1 And caretIndex <= point Then
e.Handled = False
End If
'If the character is a decimal make sure it wouldn't
'create more than 2 decimal places. len - caretIndex > 2
If e.KeyChar = "." And (len - caretIndex) > 2 Then e.Handled = True
If e.KeyChar <> "." And len - point = 1 Then e.Handled = False
If e.KeyChar = "." And (len - caretIndex) = 2 And point = -1 Then e.Handled = False
End If
End If
'Set Max Value to "999999.99"
Try
If len > 0 Then
If e.Handled = False And CDbl(tb.Text.Insert(caretIndex, e.KeyChar.ToString)) <= 999999.99 Then
e.Handled = False
Else
e.Handled = True : Exit Sub
End If
End If
Catch ex As Exception
e.Handled = True
End Try
End Sub
''' <summary>
''' Ensures the format of the entered currency value is correct
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox)
Dim txt As String = Trim(tb.Text)
Dim len As Integer = txt.Length
Dim decimalPresent As Boolean = txt.Contains(".")
Dim decimalLoc As Integer = txt.IndexOf(".")
'If no decimal present add it
'and two 0's after
If Not decimalPresent Then
txt = txt & ".00"
ElseIf decimalPresent And decimalLoc = len - 1 Then
txt = txt & "00"
End If
'Reset Variables
len = txt.Length
decimalPresent = txt.Contains(".")
decimalLoc = txt.IndexOf(".")
'If value in .1 or 2.2 add last 0
If decimalPresent And (len - 2) = decimalLoc Then
txt = txt & "0"
End If
'Reset Variables
len = txt.Length
decimalPresent = txt.Contains(".")
decimalLoc = txt.IndexOf(".")
'if value in the form .10 .12 .00 inset 0 before the decimal
If decimalPresent And decimalLoc = 0 Then
txt = "0" & txt
End If
tb.Text = txt
End Sub