|
-
Feb 27th, 2001, 02:19 PM
#1
Thread Starter
Addicted Member
How can i make the TextBox to only accept numbers?
Thanks in advance!
-
Feb 27th, 2001, 02:21 PM
#2
Monday Morning Lunatic
In a module:
Code:
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_STYLE = (-16)
Public Const ES_NUMBER = &H2000&
Public Sub SetNumber(txtNumberText As TextBox, bNumOnly As Boolean)
Dim lCurStyle As Long
Dim lNewStyle As Long
lCurStyle = GetWindowLong(txtNumberText.hwnd, GWL_STYLE)
If bNumOnly Then
lCurStyle = lCurStyle Or ES_NUMBER
Else
lCurStyle = lCurStyle And (Not ES_NUMBER)
End If
lNewStyle = SetWindowLong(txtNumberText.hwnd, GWL_STYLE, lCurStyle)
txtNumberText.Refresh
End Sub
Then just use:
Code:
SetNumber Text1, True
...to set it only numbers.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 27th, 2001, 02:24 PM
#3
_______
<?>
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'allow for special characters & backspace
If Not (IsNumeric(Chr(KeyAscii))) _
And KeyAscii <> 8 Then KeyAscii = 0
If KeyAscii < 33 Then Exit Sub
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Feb 27th, 2001, 02:28 PM
#4
New Member
Textbox with numbers-only
One easy way to do this, is read, what user types and check if it's numeric...
Code:
sub Text1_Change...
on error resume next
if IsNumeric(text1.text) = False then
if IsNumeric(Right(Text1.Text,1)) = False then
Text1.Text = Left(Text1.Text,(Len(Text1.Text)-1))
Beep
end if
end if
This code checks 'newest' char if textbox. This is working way (i think, didn't test). 
I think, that that API-way is powerful and easy way, and this is slow and no-so-sure way. But if you got nothing, you need to do something =)
GAMES, PROGRAMS, ACTIVEX CONTROLS!
ALL, WHAT WE NEED 
-
Feb 27th, 2001, 02:36 PM
#5
one line baby!
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 27th, 2001, 03:05 PM
#6
geoff, that will accept numbers, but what if you hit backspace? Nothing will happen. Use this code so you can delete as well.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
If Not (IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
End Sub
-
Feb 27th, 2001, 03:21 PM
#7
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 33 Then If Not (Chr(KeyAscii) Like "#") Then KeyAscii = 0
End Sub
-
Feb 27th, 2001, 03:29 PM
#8
Matthew! C'mon! Who the hell uses backspace?? I dont make mistakes so I don't use it!

Picky picky picky!
LOL!
to much of my thought has been going into my code coloring prog...
cant think on such simple levels anymore!! LOL
here...
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 32 Then Exit Sub Else If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
End Sub
Last edited by Static; Feb 27th, 2001 at 03:33 PM.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 27th, 2001, 04:01 PM
#9
Thread Starter
Addicted Member
Thanks a lot everyone!
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
|