Results 1 to 6 of 6

Thread: Numeric Format

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    Numeric Format

    Hi, I try this code to avoid non-numeric entry to a textbox and format it by "#.#0" but it doesn't work..

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    If Not IsNumeric(TextBox1.Text) Then
    SendKeys.Send(vbBack)
    End If
    TextBox1.Text = Format(TextBox1.Text, "#.#0")
    End Sub

    What code should I write?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What part's not working?

  3. #3
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Friend, to avoid non numeric entries is not so easy. You have to consider that is possible to put more than one char in a single entry, using Ctrl+V with something stored in clipboard. So you have for example, keep readonly your textbox, until you have checked the input char is good, then lock it again when text is changed. Anyway if you don't want to consider this problem, the easiest way to permit only numeric entries is to act only in KeyPress event routine.
    If you want to format during user's input (in real time) you have to calculate the new position of cursor and place it there.
    It's possible to format in real time, but is not easy; that's the reason because the most of programmers format when you leave the textbox.I don't know which is the problem you have with your code but surely you must keep these points in mind.
    Live long and prosper (Mr. Spock)

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I remember coming across some code a while back which handled the CTRL+V as well.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Ok, I have remembered a thread that could be useful, I hope

    http://www.vbforums.com/showthread.p...hreadid=290424

    Good Job!
    Live long and prosper (Mr. Spock)

  6. #6
    Member
    Join Date
    Apr 2004
    Posts
    44
    I like to use this sub. I call it from the KeyDown Event in a textbox. It's straight forward and easy to follow:

    VB Code:
    1. Private Sub OnlyNumbers(ByVal e As System.Windows.Forms.KeyEventArgs)
    2.         ' Initialize the flag to false.
    3.         nonNumberEntered = False
    4.  
    5.         ' Determine whether the keystroke is a number from the top of the keyboard.
    6.         If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
    7.             ' Determine whether the keystroke is a number from the keypad.
    8.             If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
    9.                 ' Determine whether the keystroke is a backspace.
    10.                 If e.KeyCode <> Keys.Back Then
    11.                     ' Determine whether the keystroke is a . or a - on either the main keyboard or keypad
    12.                     If e.KeyCode <> Keys.Subtract Then  'keypad
    13.                         If e.KeyCode <> Keys.Decimal Then 'keypad
    14.                             If e.KeyCode <> Keys.OemPeriod Then 'keyboard
    15.                                 If e.KeyCode <> Keys.OemMinus Then 'keyboard
    16.                                     ' A non-numerical keystroke was pressed.
    17.                                     ' Set the flag to true and evaluate in KeyPress event.
    18.                                     nonNumberEntered = True
    19.                                 End If
    20.                             End If
    21.                         End If
    22.                     End If
    23.                 End If
    24.             End If
    25.         End If
    26.     End Sub

    Then in the KeyPress Event I use:

    VB Code:
    1. ' Check for the flag being set in the KeyDown event.
    2.         If nonNumberEntered = True Then
    3.             ' Stop the character from being entered into the control since it is non-numerical.
    4.             e.Handled = True
    5. endif

    I had used a string comparison before. But a moron that I work for decided it was too cryptic. So now I use this one all the time.

    Do you also want to round it to the nearest tenth and add a zero? Or would typing in 1 2 result in 1.20 ?

    Jim
    Last edited by yaksplat; Jun 4th, 2004 at 06:25 AM.

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