I know that it related some how to keypress event ,
but I tryed and couldn't worked it out.
Printable View
I know that it related some how to keypress event ,
but I tryed and couldn't worked it out.
click on the text box, then goto the properties window, and click on Data Format, and chose numbers
well , it doesn't work !!
again I need that the user want be able to insert numbers
to the textbox.
ok, this isn't the best but here it is (no gaurantee):
[code]
'put this into the declerations:
dim inttxt as interger
' end declerations
'put this into the text change function:
if text1.text = "0", or "1", or "2", or "3", or "4", or "5", or "6", or "7", or "8", or "9" then
text1.visible = true ' just a command to waste time
inttxt = text1.text
else:
text1.text = ""
'this might not work
There are a lot of ways to do this, but you should find that this is pretty bullet proof. What you do is just check the Asc entered into the box on keypress. In this example, Text1.text will only accept 1-9, a backspace (character 8) and a space (character 32).
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
CheckChar = KeyAscii
If CheckChar = 8 Then
Exit Sub
ElseIf CheckChar = 32 Then
Exit Sub
ElseIf CheckChar < 48 Or CheckChar > 57 Then
MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
KeyAscii = 0
Text1.Text = Text1.Text & Chr(KeyAscii)
End If
End Sub
Hope that this helps
(Using VB 6 SP 3)
Well, you can use the Isnumeric funtion too.
This should be secure.
Code:Private Sub Text1_Change()
If IsNumeric(Text1.Text) Then
'Code when it is
'(Nothing)
Else
'Code when it's not
MsgBox "Only numbers! Try again!"
Text1.Text = ""
Text1.SetFocus
End If
End Sub
Also, just pointing out.
The method which reeset refers too wouldn't allow the user to paste a number from the clipboard.
Good point. But you could easily fix that by just adding exeptions to new values.
Thanks for pointing that out.Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
CheckChar = KeyAscii
If CheckChar = 8 Then
Exit Sub
ElseIf CheckChar = 32 Then
Exit Sub
ElseIf CheckChar = 22 Then
'If user tries to paste
svalue = Clipboard.GetText
For x = 1 To Len(svalue)
sNextChar = Asc(Mid(svalue, x, 1))
If sNextChar < 48 Or sNextChar > 57 Then
MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
Clipboard.Clear
Exit Sub
End If
Next
ElseIf CheckChar = 3 Then
'if user try's to copy
Exit Sub
ElseIf CheckChar < 48 Or CheckChar > 57 Then
MsgBox "You can only enter a numeric in this box", vbApplicationModal, "Numeric violation"
KeyAscii = 0
Text1.Text = Text1.Text & Chr(KeyAscii)
End If
End Sub
This code will work with negative values and commas
Code:Private Sub Text1_Change(): Dim temp&
temp = Text1.SelStart
If IsNumeric(Text1) Then Text1.Tag = Text1 Else Text1 = Text1.Tag
Text1.SelStart = temp
End Sub
I know this can be done just with a simple API function, but I can remeber the Function name, if anyone found it just email.
Thanks
VbSquare posted a tip about doing this same thing. Here's the code:
Well, there ya go! Works no problem.Code:'Declarations
Public Declare Function GetWindowLong& Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
Public Declare Function SetWindowLong& Lib "user32" Alias _
"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long)
Public Const ES_NUMBER = &H2000&
Public Const GWL_STYLE = (-16)
'Textbox Change
Private Sub Text1_Change()
Dim tmpValue&
Dim fAlignment&
Dim ret&
fAlignment& = ES_NUMBER
tmpValue& = GetWindowLong&(txtRefreshRate.hwnd, GWL_STYLE)
ret& = SetWindowLong&(txtRefreshRate.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
txtRefreshRate.Refresh
End Sub
Wow, such overkill!
try this:
hehe. Easy.Code:text1.text = val(text1.text)
Thanks Xero! that the power of API. :)