how to use the keypress function is visual basic 6

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 23
Reputation: dc_24l is an unknown quantity at this point 
Solved Threads: 0
dc_24l dc_24l is offline Offline
Newbie Poster

how to use the keypress function is visual basic 6

 
0
  #1
Nov 19th, 2007
hey need some help guys. for example i have a text box and i will have input in it. but only numeric ones are allowed to be inputted in the text box if a character for example "a" is pressed the text box will not change and type the "a" and if a numeric button for example is 1, the text box will have 1. how can i do this? thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: vivekPSG is an unknown quantity at this point 
Solved Threads: 1
vivekPSG vivekPSG is offline Offline
Newbie Poster

Re: how to use the keypress function is visual basic 6

 
0
  #2
Jan 5th, 2008
hi dc_241

try this
if keyascii>65 or keyascii<97
msgbox"error"
endif
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: how to use the keypress function is visual basic 6

 
0
  #3
Jan 5th, 2008
try this following code :

' This code for string input or other (comma,drop,question mark etc) except numeric
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. If KeyAscii < 48 Or KeyAscii > 57 Then
  3. Text1.Locked = False
  4. Else
  5. Text1.Locked = True
  6. End If
  7. End Sub

' This code for numeric input only, other input cannot be written
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text2_KeyPress(KeyAscii As Integer)
  2. If KeyAscii < 48 Or KeyAscii > 57 Then
  3. Text2.Locked = True
  4. Else
  5. Text2.Locked = False
  6. End If
  7. End Sub

' This code for string input only (drop, comma, question mark cannot be written)
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text3_KeyPress(KeyAscii As Integer)
  2. If KeyAscii > 65 Or KeyAscii > 97 Then
  3. Text1.Locked = False
  4. Else
  5. Text1.Locked = True
  6. End If
  7. End Sub

actually you can combine this with ascii code. look the code in ascii table and you can let what input can be written.

OK.hope this helps..
Last edited by Jx_Man; Jan 5th, 2008 at 8:02 am. Reason: code tags
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 106
Reputation: SCBWV is an unknown quantity at this point 
Solved Threads: 16
SCBWV SCBWV is offline Offline
Junior Poster

Re: how to use the keypress function is visual basic 6

 
1
  #4
Jan 5th, 2008
You don't have to lock the text box, just set KeyAscii to 0. Also, you need to handle the backspace key to allow users to fix errors:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
'Let these key codes pass through
Case Else
'All others get trapped
KeyAscii = 0
End Select
End Sub
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: how to use the keypress function is visual basic 6

 
0
  #5
Jan 5th, 2008
thx SCBWV...
nice recovery for my post...
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 7
Reputation: Neophyte08 is an unknown quantity at this point 
Solved Threads: 0
Neophyte08 Neophyte08 is offline Offline
Newbie Poster

Re: how to use the keypress function is visual basic 6

 
0
  #6
Jan 7th, 2008
Try this code if it help...

If (keyascii<48 or keyascii>57) and keyascii<>vbkeyback then
keyascii=0
end if
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: KSS is an unknown quantity at this point 
Solved Threads: 1
KSS's Avatar
KSS KSS is offline Offline
Newbie Poster

Re: how to use the keypress function is visual basic 6

 
0
  #7
Mar 29th, 2009
You can also try the isnumeric() function at the keypress event...
For example...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0
End Sub
Last edited by KSS; Mar 29th, 2009 at 5:38 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC