Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

How to close a form without using command button?. I need to close a form through
keyboard. Can anyone help me for this regards?

Thanks and Regards,
A.Harisankar
Posted

Alt+F4.

Should work in all cases without any programming effort, and it's the best not to disable it.
You already have the Answer on programmatic Close by Rob, see also my comments to his Answer.

Any problems?
 
Share this answer
 
v2
Comments
Nish Nishant 21-Jan-11 13:15pm    
5d.

Proposed as answer.
Espen Harlinn 21-Jan-11 13:35pm    
5+ Correct, nice, and simple :)
You can close a form with err... Close(). You can probably add a key press event to check for the key and stick the close in there. What key do you want to close the form? That may not work if you have child controls focused, in which case you need to hook the (presuming this is WinForms and not WPF) underlying WNDPROC - which I think is on PreProcessControlMessage.

Of course, you shouldn't do this as you're going away from the Windows design guidelines and creating an application which doesn't behave in the way people are used to.
 
Share this answer
 
Comments
Dylan Morley 21-Jan-11 8:46am    
Yep, agreed. Design guidelines are so important, standard stuff like closing windows should be instantly familiar to the user. Click a 'Close' button on your form or click the 'Close' window command in the non-client area. Shouldn't really mess with that behaviour.
Sergey Alexandrovich Kryukov 21-Jan-11 13:20pm    
Rob, it is easier. You just need to add handler to KeyPress on appropriate control(s). You can also add a button or even a menu ("Exit") -- by default they all can be navigated via keyboard, that satisfies keyboard-only requirement.
Sergey Alexandrovich Kryukov 21-Jan-11 13:22pm    
But yes, PreProcessControlMessage help to preview event in form, so it will work if children do not add proper handlers...

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