views:

15

answers:

1
{Form constructor}
this->KeyDown += gcnew KeyEventHandler(this, &Form::Form_KeyDown);
...

void Form1::Form_KeyDown(Object^ Sender, KeyEventArgs^ E)
{
   MessageBox::Show("Key = " + E->KeyCode.ToString(), "Test");
}

The above event handler never fires. But the form's child controls' handler does. What would be the problem ?

+1  A: 

In addition to having your event handler, you need to set the form's KeyPreview property to true. According to MSDN:

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus.

Chris Schmich
Thanks for the extremely quick answer!
shadeMe