|
-
Jul 19th, 2003, 11:21 AM
#1
Thread Starter
New Member
Keypress & Keydown Event Handling
I am new to VB.Net and would like to create keystroke event handling at the Form level. I understand that the Form's KeyPreview property must be set to True. I want to intercept the <Enter> key even when Button objects are on the form. I create two event handlers within the form's code something like this:
Sub KPress(ByVal o as Object, ByVal e as KeyPressEventArgs) Handles MyBase.KeyPress
e.Handled = True
msgbox("Form Intercepted the KeyPress")
End Sub
and
Sub KDown(ByVal o as Object, ByVal e as KeyEventArgs) Handles MyBase.KeyDown
e.Handled = True
msgbox("Form Intercepted the KeyDown")
End Sub
However, when a Button has the Focus, the Button's Click event handles the keystroke, not the Form.
Thanks for your help...in advance,
-
Jul 19th, 2003, 12:04 PM
#2
Sleep mode
Isn't this helpful. . .
VB Code:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Asc(e.KeyChar()) = 13 Then
'Do something
e.Handled = False
End If
End Sub
Last edited by Pirate; Jul 19th, 2003 at 12:20 PM.
-
Jul 19th, 2003, 07:28 PM
#3
Thread Starter
New Member
Thanks, Pirate, but that doesn't work if a Button object has the focus. The code that I had written works so long as the object that has the focus is NOT a button. It works for text boxes and other controls that normally allow keyboard activity, but not for Buttons. The default behaviour for a button object is, of course, to execute the OnClick event when the <Enter> key is pressed. Maybe the solution is to override the default behaviour for the button, but I don't know how.
-
Jul 20th, 2003, 01:22 PM
#4
Sleep mode
As an example for overriding events , it's done this way :
(dunno what you are trying to do though )
VB Code:
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
If Asc(e.KeyChar()) = 13 Then
'do this
End If
End Sub
-
Jul 21st, 2003, 10:48 AM
#5
Thread Starter
New Member
Sorry, Pirate,
According to your quote, I am becoming a better man every day, because I must be making alot of mistakes!! Anyway, I had already tried the overrides, but I still cannot get it to work. I have found a work-around, but am still curious as to how to do this. Are you saying that you have created a form, placed a button on the form as the only control and when the button has the focus and you press the <Enter> key, that the button's Click event does not get triggered, but the form's keypress or keydown events get triggerered instead?
Thanks again...
-
Jul 21st, 2003, 02:00 PM
#6
Sleep mode
lol ,
I know the code I posted doesn't solve your problem . You said how to override an event in VB.NET , so basically that was only an example on how to do so . OK , I built a small demo with one form and one button , and yes it triggers the form's event but not the button's keypress . In this situation , APIs are *sometimes* more reliable than .NET ways . Did you try something with APIs ?
-
Jul 22nd, 2003, 09:39 AM
#7
Thread Starter
New Member
Pirate,
Your last posting stated that you were successful, however you go on to say that APIs calls are the way to go. Were you, in fact, successful, or should the words "form" and "button" be swapped in your posting? If you were successful, then why would an API call be necessary?
However, to answer your question...no, I have not tried any API calls. I decided to place a textbox on the form and insure that the focus always returns to that textbox control after any event. That way, the textbox will handle all key events, not the form.
Thanks for your time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|