|
-
Aug 13th, 2002, 02:44 PM
#1
Thread Starter
Frenzied Member
Textbox that ONLY shows numbers ??
How do i make a textbox that you ONLY can write numbers in ?
I thought of something like this:
VB Code:
If txtNumbers.Text = "a" or "A" Then
txtNumbers.Text = ""
End If
It works fine, but it will just be a hell of a lot of work !
And i'm... pretty lazy
Regards
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Aug 13th, 2002, 02:48 PM
#2
Hyperactive Member
Try this:
VB Code:
Private Sub txtTextBox_KeyPress(KeyAscii As Integer)
If (KeyAscii = 8) Or (KeyAscii = 32) Then Exit Sub
If IsNumeric(Chr(KeyAscii)) = False Then
KeyAscii = 0
Exit Sub
End If
End Sub
I didn't test is, but I think this is what I usually use.
-
Aug 13th, 2002, 02:49 PM
#3
PowerPoster
-
Aug 13th, 2002, 02:52 PM
#4
Thread Starter
Frenzied Member
Thanks
Thanks dude !
It works perfectly ! i will look it up laters
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Aug 13th, 2002, 03:11 PM
#5
-= B u g S l a y e r =-
Re: Thanks
Originally posted by vbNeo
Thanks dude !
It works perfectly ! i will look it up laters
until the user paste something into the textbox
-
Aug 13th, 2002, 03:12 PM
#6
PowerPoster
This neat snippet will make it the "correct way" 
VB Code:
'Consts
Private Const GWL_Style As Integer = (-16)
Private Const ES_Number As Long = &H2000&
'Declares
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Sub SetNumbersOnly(iTextBox As TextBox, Optional iActive As Boolean = True)
Dim Temp As Long
'Get the current style
Temp = GetWindowLong(iTextBox.hWnd, GWL_Style)
'Set the new style
SetWindowLong iTextBox.hWnd, GWL_Style, IIF(iActive, Temp Or ES_Number, Temp And (Not ES_Number))
End Function
Last edited by Fox; Aug 13th, 2002 at 03:16 PM.
-
Aug 13th, 2002, 03:22 PM
#7
-= B u g S l a y e r =-
you can still paste nonenumeric data into the textbox
-
Aug 13th, 2002, 03:26 PM
#8
The picture isn't missing
that's why they invented sublcassing
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
Aug 13th, 2002, 03:29 PM
#9
PowerPoster
catch any changes via the change event & then run isNumeric
-
Aug 13th, 2002, 03:32 PM
#10
PowerPoster
actually, that was a little too flip. I don't think isNumeric will work on long strings, and the thing about being able to past non-numeric was a good catch against my solution.
-
Aug 13th, 2002, 03:54 PM
#11
So Unbanned
Muwhahaha!
In the change event put:
Text1.Text = Val(Text1.Text)
-
Aug 13th, 2002, 03:58 PM
#12
So Unbanned
Either that or do a val command manually:
VB Code:
Function NumData(byval StrDat as String) as string
validchrs = "01234567890.-+"
For X = 1 to len(strdat)
if instr(validchrs,mid$(strdat,x,1)) > 1 then newstr = newstr & mid$(strdat,x,1)
Next
NumData = newstr
End Function
' use like:
' in change event
text1.text = numdata(text1.text)
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
|