Computing.Net > Forums > Programming > vb event doubt

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

vb event doubt

Reply to Message Icon

Name: john
Date: November 26, 2002 at 22:13:01 Pacific
OS: win 98
CPU/Ram: Cel/64 Mb
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: November 27, 2002 at 01:04:06 Pacific
Reply:

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


0

Response Number 2
Name: john
Date: November 27, 2002 at 01:51:56 Pacific
Reply:

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


0

Response Number 3
Name: HiJinx
Date: November 27, 2002 at 03:43:58 Pacific
Reply:

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 If

Text1.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 If

End Sub


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Help in c VB comparison



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: vb event doubt

Question on VB events www.computing.net/answers/programming/question-on-vb-events/6965.html

VB event log script www.computing.net/answers/programming/vb-event-log-script/12366.html

vb mshflexgrid doubt www.computing.net/answers/programming/vb-mshflexgrid-doubt/4538.html