|
-
Jun 4th, 2013, 04:23 PM
#1
Thread Starter
Hyperactive Member
Numbers Only and - Only
Hi All
I am try to only type numbers in a textbox and stop letters from going into textbox which works fine, But I also want ------- to appear in textbox I looked up on the internet and should be vbKeySubtract so I added to one of the case lines but don't work just beeps
Can someone please help me out with this thanks
http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Accepts only numeric input
Select Case KeyAscii
Case vbKey0 To vbKey9
Case vbKeyBack, vbKeyClear, vbKeyDelete, vbKeySubtract
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Kind Regards
Steve
-
Jun 4th, 2013, 04:28 PM
#2
Re: Numbers Only and - Only
The Ascii value for the minus sign is 45.
-
Jun 5th, 2013, 02:04 AM
#3
Hyperactive Member
Re: Numbers Only and - Only
put it on keypress event
Const key = "1234567890"
If KeyAscii <> 0 Then
If InStr(key, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End If
-
Jun 5th, 2013, 07:13 AM
#4
Re: Numbers Only and - Only
Per Markt....example
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9
Case 45
Case vbKeyBack, vbKeyClear, vbKeyDelete ', vbKeySubtract---remove
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
Case Else
KeyAscii = 0
Beep
End Select
End Sub
EDIT: FYI, vbKeySubtract is applicable in either KeyDown or KeyUp events but not
on a KeyPress event.
Last edited by SamOscarBrown; Jun 5th, 2013 at 07:26 AM.
-
Jun 5th, 2013, 07:19 AM
#5
Re: Numbers Only and - Only
..... and never forget, that in cases like this, a user still can copy&paste whatever characters he wishes into the Textbox......
How about trapping the CTRL+V-Combination?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 5th, 2013, 10:18 AM
#6
Thread Starter
Hyperactive Member
Re: Numbers Only and - Only
@zoni
How do you trap ctrl v
@Samoscarbrown
I will give that a try later thanks
-
Jun 5th, 2013, 11:21 AM
#7
Re: Numbers Only and - Only
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 5th, 2013, 11:50 AM
#8
Re: Numbers Only and - Only
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
|