Author Message

i am working on a simple calculator and have it functionaly finished
no problem
I am using 2 text boxes for the input and a Command button to perform
the calculation (ie add, subtract, etc)
what i am trying to do is limit the input to numbers and the symbols
"." and "-", and also allow the use of backspace
the code i am using so far is;

Private Sub txtNum1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then KeyAscii = 8
If KeyAscii = Asc("-") Then KeyAscii = Asc("-")
If KeyAscii = Asc(".") Then KeyAscii = Asc(".")
If KeyAscii > Asc("9") Then _
End Sub

this managws to stop the use of letters but it still allows symbols
such as /, *, +, etc etc
What am i doing wrong or id there any easier way to code it.
It may be obvious that i am no expert so any help is greatly
appreciated thanx

-=*<{*filter*}ioN>*=-

Sun, 25 Nov 2001 03:00:00 GMT

Quote:

>i am working on a simple calculator and have it functionaly finished
>no problem
>I am using 2 text boxes for the input and a Command button to perform
>the calculation (ie add, subtract, etc)
>what i am trying to do is limit the input to numbers and the symbols
>"." and "-", and also allow the use of backspace
>the code i am using so far is;

>Private Sub txtNum1_KeyPress(KeyAscii As Integer)
>If KeyAscii = 8 Then KeyAscii = 8
>    If KeyAscii = Asc("-") Then KeyAscii = Asc("-")
>    If KeyAscii = Asc(".") Then KeyAscii = Asc(".")
>    If KeyAscii > Asc("9") Then _
>End Sub

>this managws to stop the use of letters but it still allows symbols
>such as /, *, +, etc etc
>What am i doing wrong or id there any easier way to code it.
>It may be obvious that i am no expert so any help is greatly
>appreciated thanx

This will work

if (KeyAscii < 48 or KeyAscii > 57) then
If (chr\$(KeyAscii) <> "-" and chr\$(KeyAscii) <> ".") then KeyAscii = 0
end if

Sun, 25 Nov 2001 03:00:00 GMT

-if (KeyAscii < 48 or KeyAscii > 57) then
-    If (chr\$(KeyAscii) <> "-" and chr\$(KeyAscii) <> ".") then
KeyAscii = 0
-end if

thank you very much
but is there a way to do this with Select Case?
i'm just curious how it would be done this way as well

-=*<{*filter*}ioN>*=-

Sun, 25 Nov 2001 03:00:00 GMT
There's a beautiful way to accept only the characters you want:

if instr(1,"1234567890.-+*/",chr(keyascii)) =0 then keyascii=0

HTH,
aris.

Sun, 25 Nov 2001 03:00:00 GMT

Quote:
>There's a beautiful way to accept only the characters you want:

>if instr(1,"1234567890.-+*/",chr(keyascii)) =0 then keyascii=0

>HTH,
>aris.

I've never thought of that, cool.

Mon, 26 Nov 2001 03:00:00 GMT

Quote:

>what i am trying to do is limit the input to numbers and the symbols
>"." and "-", and also allow the use of backspace

If Not (KeyAscii = 8 Or InStr("1234567890.-", Chr\$(KeyAscii)) <> 0) Then KeyAscii = 0

J. Bodie

Mon, 26 Nov 2001 03:00:00 GMT
That's what I should have written, but I was in a hyrry.

Alternatively, you can use "0123456789.+-*/" & chr(8) as a string instead of
using 'or keyascii=8'

If there's a default button this will work,  but maybe we have to allow
Enter(13) as well.

Aris.

Mon, 26 Nov 2001 03:00:00 GMT

-if (KeyAscii < 48 or KeyAscii > 57) then
-    If (chr\$(KeyAscii) <> "-" and chr\$(KeyAscii) <> ".") then
KeyAscii = 0
-end if

thank you very much
but is there a way to do this with Select Case?
i'm just curious how it would be done this way as well

-=*<{*filter*}ioN>*=-

hi I'm a new user

Select Case KeyAscii

Case 48 To 57
'do not delete

Case 45
'do not delete

Case 46
'do not delete

Case Else
KeyAscii = 0

End Select

Wed, 28 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages