Form_Keydown being overridden

G

Guest

I'm having a weird problem with the Keydown event.

- I have a form with the KeyPreview property set to true
- I have a Form_KeyDown sub that does a bit of code for me
- I have a few control buttons on the form

Now comes the weird part. When I first launch the form, it intercepts the
key presses no problem (i'm capturing the arrow keys). However, when I click
on a button, the form no longer fires off the keydown event, pressing the
left arrow, for example, will start cycling through the other buttons.

Is there a way to return the focus or control to the form so it can capture
the arrow's being pressed and run the form_keydown code?
 
S

Shane Story

There is a property on the Form that must be set.. I think it is KeyPreview
(I don't have VS up and running now). That allows the form to see it before
a control.

What happens is the control is handling it and thus eating the event before
your form ever sees it.

HTH,

Shane
 
R

R. MacDonald

Hello, Jeffy210,

When the focus is on a Button, the arrow keys are being "pre-processed"
as navigation keys so they never reach the form's KeyDown event.

Two possibilities come to mind.

1. You could replace the Button control with your own button, inherited
from the standard button, but include an override like:

Protected Overrides Function _
IsInputKey(ByVal keyData As Keys) As Boolean
Select Case keyData
Case Keys.Down, Keys.Left, Keys.Right, Keys.Up
Return True
Case Else
Return False
End Select
End Function

2. Maybe you could "catch" the arrow keys before they are pre-processed
by overriding WndProc in your form. I have never tried this, though.
But maybe others more learned may be able to suggest how to do this, or
offer other ways around the problem.

Cheers,
Randy
 
G

Guest

Okay, I understand what you mean, and I even found the "UserControl" item,
but for the life of me I can't figure out how to make it inherit the
properties of a standart button. Addionally that means I'm having trouble
figuring out where to put that code.

But all things being said, it looks like that will be the exact solution for
what I'm looking for. Thanks!
 
G

Guest

Okay, actually bothered to sit down and read on User Controls and I got it
working. That rocks! Thank you very much. I can see some good uses for this
in the future.

Thank you very much!
Jeffy210
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top