Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
freinds pls can anyone say me the difference bet Keydown event and keypress event.
which one executes first when a key is pressed.
Pls note that in keydown event i hav written the coding that if del key is pressed then delete the contents else nothing
and in keypress event i hav written code so that user can enter only integers.but the problem is sometime while entering numbers it works fine but sometimes nothing can be entered
even integers.iam thing when a key is pressed, keycode event executes and it checks whether del key is pressed and the result will be no and it come out without doing anything
but how it works fine sometimes
pls help
reply as early as possible

In Windows, there are keydown and keyup events. VB and the .Net languages also add the keypress event, which is sort of an extra keydown.
Keydown is more limited, since it only fires when a character-key is pressed (letters, numbers, punctuation). Keys like Shift and F4 are ignored.
Keyup and keydown give information about any key pressed, including combinations of keys, Shift, Alt, etc.
The order is keydown then keypress, and then keyup when the key is released. You do not need to handle both keydown and keypress events for the same control, and the keydown event can even block the keypress event (which might be happening for you).
If you only needed to handle characters, you could just use keypress, but you need to handle the DEL key, so you might want to put all your code in the keydown event.
Cheers

Thanx!
but the event keydown takes one parameter ( keycode) and the event keypress takes one parameter(keyascii), both parameter ar not same.
then what we must do to equalize them.
Anyway Thanx for ur reliable reply
Thanx

Try cutting and pasting the following code. I tried to break it, but couldn't. Your problem could have been as simple as not resetting focus to the textbox after clearing it.
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
Text1.Text = ""
Exit Sub
End IfText1.SetFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then
'Numeric key OK
Else
If KeyAscii = 46 Or KeyAscii = vbKeyBack Then
'decimal and BackSpace OK
Else
Beep
KeyAscii = 0
End If
End IfEnd Sub

![]() |
Help in c
|
VB comparison
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |