Nitin Ware
Nitin Ware

Reputation: 139

keyUp event not working [WinForm]

I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control

Upvotes: 2

Views: 971

Answers (1)

L. Guthardt
L. Guthardt

Reputation: 2056

Your issue is that the event MainForm_KeyUp does not even get fired on your key up, because you have focues another control. But you can fix that with KeyPreview.

A Form object has the property KeyPreview. According to the MSDN:

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

So when you set:

this.KeyPreview = true;

You enable that your MainForm gets notified about those key events always. Even when any other Control is focused. So you enable that those key events will invoke MainForm_KeyUp().

Now set a breakpoint:

private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
    //set a breakpoint here, so you get confirmation, that the event will get fired 
    //on key up of the *delete* button

    //...now do what you desire
}

Upvotes: 5

Related Questions