Solved

key disabling

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

21 Experts available now in Live!

Get 1:1 Help Now