Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I make a control handle the arrow keys, instead of having arrow keys automatically handled by the framework's focus management?

By default, the arrow keys are not handled by a control's key processing code, but instead are filtered out for focus management. Hence, the control's KeyDown, KeyUp and KeyPressed events are not raised when you press an arrow key. If you want a control to handle these keyboard events, then override the control's IsInputKey method.

using System.Windows.Forms;

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

You can then process the keys by overriding the control's OnKeyDown method.

Contributed from George Shepherd's Windows Forms FAQ



Page view counter