Trapping arrow keys in UserControl

V

Vincent

Hi,

I have a user control that needs to trap the arrow keys to move items
around internally. However, using the arrow keys will move the focus to
another control on the form hosting the user control.

How do I stop this?

Vincent.
 
C

chanmm

Maybe you want to check again because when I move my arrow keys the focus
won't lost inside my control.

chanmm
 
V

Vincent

Nope, it definitely happens.

If you make an owner-drawn user control (with no controls on it), and
put it on a form, with other controls (say a button), then using the
arrow keys will move focus to the other control.

However, I have found the solution after Googling for hours, it is to
override the IsInputKey method of the UserControl like so:

protected override bool IsInputKey(Keys key)
{
switch(key)
{
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Left:
return true;
}
return base.IsInputKey(key);
}

Which I haven't tried yet because I'm on linux at the moment (and I
don't have mono), but it looks feasible.

Vincent.
 

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