Solved

key disabling

Posted on 2004-08-17
3
745 Views
Last Modified: 2013-12-25
hello

I'm new to VB programming.

I want to block any non-digit entries into a textbox.
It will work fine if i depend upon keypress event. For example,

Private Sub Text1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
   Case 48 To 57
     'Allow keystroke
   Case Else
      KeyAscii = 0
End Select

End Sub

But, if I try keydown event it won't work.

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   
   Select Case KeyCode
      Case vbKey0 To vbKey9
         'Allow the key stroke
      Case Else
         KeyCode = 0
   End Select

End Sub

The question is, Why? Whats lacking in keydown?

0
Comment
Question by:wide-eyes
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
JR2003 earned 50 total points
ID: 11819553
The object with the focus receives the event. A form can receive the event only if it has no visible and enabled controls or if the KeyPreview property is set to True. A KeyPress event can involve any printable keyboard character, the CTRL key combined with a character from the standard alphabet or one of a few special characters, and the ENTER or BACKSPACE key. A KeyPress event procedure is useful for intercepting keystrokes entered in a TextBox or ComboBox control. It enables you to immediately test keystrokes for validity or to format characters as they're typed. Changing the value of the keyascii argument changes the character displayed.

You can convert the keyascii argument into a character by using the expression:

Chr(KeyAscii)

You can then perform string operations and translate the character back to an ANSI number that the control can interpret by using the expression:

KeyAscii = Asc(char)

Use KeyDown and KeyUp event procedures to handle any keystroke not recognized by KeyPress, such as function keys, editing keys, navigation keys, and any combinations of these with keyboard modifiers. Unlike the KeyDown and KeyUp events, KeyPress doesn't indicate the physical state of the keyboard; instead, it passes a character.

KeyPress interprets the uppercase and lowercase of each character as separate key codes and, therefore, as two separate characters. KeyDown and KeyUp interpret the uppercase and lowercase of each character by means of two arguments: keycode, which indicates the physical key (thus returning A and a as the same key), and shift, which indicates the state of shift+key and therefore returns either A or a.

If the KeyPreview property is set to True, a form receives the event before controls on the form receive the event. Use the KeyPreview property to create global keyboard-handling routines.

Note   The ANSI number for the keyboard combination of CTRL+@ is 0. Because Visual Basic recognizes a keyascii value of 0 as a zero-length string (""), avoid using CTRL+@ in your applications.

0
 
LVL 18

Expert Comment

by:JR2003
ID: 11819570
The difference is between KeyCode and KeyAscii.
KeyCode allows you see whats been pressed.
KeyAscii gives you the character that's been pressed and allows you to change it.
0
 

Author Comment

by:wide-eyes
ID: 11820087

Thanku guyz, for ur fast and to the point explanations..
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question