Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backspace key to return previous form

Tags:

c#

winforms

I want to return to previous form in C# using backspace key. I am using KeyDown event on form to check for backspace key . but the form is not detecting any key down event.

How can I achieve this?

private void History_P_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData.ToString() == "Back")
        MessageBox.Show("Back button pressed");
}

where History_P is form name.

like image 826
lionheart Avatar asked Apr 02 '26 03:04

lionheart


1 Answers

You need to enable the KeyPreview property on the Form.

With this property the key being pressed (all KeyUp, KeyDown, KeyPressed events) will first be caught by the Form and is then passed on to the control that is focused at that moment, unless you set the KeyPressEventArgs.Handled to true.

like image 109
Gerald Versluis Avatar answered Apr 03 '26 16:04

Gerald Versluis