Solved

key disabling

Posted on 2004-08-17
3
743 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now