Question about preventing certain input 
Author Message
 Question about preventing certain input

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  
 Question about preventing certain input

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  
 Question about preventing certain input


-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  
 Question about preventing certain input
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  
 Question about preventing certain input


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  
 Question about preventing certain input

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  
 Question about preventing certain input
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  
 Question about preventing certain input



-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  
 
 [ 8 post ] 

 Relevant Pages 

1. Preventing input of certain chars to text boxes??

2. preventing certain rows from printing

3. Prevent User to Browse Certain Folder

4. Only allowing certain input

5. strip form input of certain characters

6. Text edit control that limits input to certain chars

7. mask edit & locking (preventing input)

8. prevent user input in textbox

9. Preventing Input

10. Disable/prevent input in textbox

11. procedure to find certain files from a certain date on harddisk

12. Newbie Question- Preventing multiple instances of a program from loading

 

 
Powered by phpBB® Forum Software