|
-
Apr 30th, 2003, 10:34 AM
#1
Thread Starter
I wonder how many charact
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
-
Apr 30th, 2003, 10:40 AM
#2
Thread Starter
I wonder how many charact
Only thing itmisses is if the first key you press is a space...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|