Results 1 to 2 of 2

Thread: Only allow 2 digits past decimal point, numeric textbox

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         'only allows appendage of digits, one decimal point, and 2 decimal places
    3.         If Not e.KeyChar.IsDigit(e.KeyChar) Then  'if not a digit
    4.             If Not Asc(e.KeyChar) = 8 Then         'and not a backspace
    5.                 If Not TextBox1.Text.LastIndexOf(Chr(46)) = -1 Then 'and a decimal has already been added
    6.                     'tell system not to send message to textbox
    7.                     e.Handled = True
    8.                 End If
    9.             End If
    10.         Else 'if a digit
    11.             'compare index of a decimal point (if it exists), and length of text
    12.             If Not TextBox1.Text.LastIndexOf(Chr(46)) = -1 Then
    13.                 If TextBox1.TextLength > TextBox1.Text.LastIndexOf(Chr(46)) + 2 Then
    14.                     'if length is over 2 spaces from decimal pt, don't
    15.                     'send message to system
    16.                     e.Handled = True
    17.                 End If
    18.             End If
    19.  
    20.         End If
    21.     End Sub

  2. #2

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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
  •  



Click Here to Expand Forum to Full Width