Only allow 2 digits past decimal point, numeric textbox
I wrote this sub for a textbox.... seems to work great, if anyone has some time and wants to TEST it..(make sure there are no flukes), I would appreciate any comments...
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'only allows appendage of digits, one decimal point, and 2 decimal places
If Not e.KeyChar.IsDigit(e.KeyChar) Then 'if not a digit
If Not Asc(e.KeyChar) = 8 Then 'and not a backspace
If Not TextBox1.Text.LastIndexOf(Chr(46)) = -1 Then 'and a decimal has already been added
'tell system not to send message to textbox
e.Handled = True
End If
End If
Else 'if a digit
'compare index of a decimal point (if it exists), and length of text
If Not TextBox1.Text.LastIndexOf(Chr(46)) = -1 Then
If TextBox1.TextLength > TextBox1.Text.LastIndexOf(Chr(46)) + 2 Then
'if length is over 2 spaces from decimal pt, don't
'send message to system
e.Handled = True
End If
End If
End If
End Sub