Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Form only to display some data which opens in ShowDialog mode. if I press the space bar I want to close this form instead of clicking on the close button. is this can be done?

thanks

What I have tried:

searched but did not find anything
Posted
Updated 27-Dec-21 9:48am

1 solution

You will need to add a KeyUp event handler to the form and populate the generated handler as below.

private void TestForm_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Space)
    {
        Close();
    }
}


Please note that this only works when the focus is on the form, if any controls in the form are focused this will not work so you might have to ensure that the controls have there TabStop property to false unless you want to add KeyUp handlers for each control.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900