|
-
Jun 4th, 2004, 03:08 AM
#1
Thread Starter
Hyperactive Member
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?
-
Jun 4th, 2004, 04:10 AM
#2
-
Jun 4th, 2004, 04:27 AM
#3
Hyperactive Member
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)
-
Jun 4th, 2004, 04:33 AM
#4
I remember coming across some code a while back which handled the CTRL+V as well.
-
Jun 4th, 2004, 04:40 AM
#5
Hyperactive Member
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)
-
Jun 4th, 2004, 06:20 AM
#6
Member
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:
Private Sub OnlyNumbers(ByVal e As System.Windows.Forms.KeyEventArgs)
' Initialize the flag to false.
nonNumberEntered = False
' Determine whether the keystroke is a number from the top of the keyboard.
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back Then
' Determine whether the keystroke is a . or a - on either the main keyboard or keypad
If e.KeyCode <> Keys.Subtract Then 'keypad
If e.KeyCode <> Keys.Decimal Then 'keypad
If e.KeyCode <> Keys.OemPeriod Then 'keyboard
If e.KeyCode <> Keys.OemMinus Then 'keyboard
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = True
End If
End If
End If
End If
End If
End If
End If
End Sub
Then in the KeyPress Event I use:
VB Code:
' Check for the flag being set in the KeyDown event.
If nonNumberEntered = True Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|