Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo,

I wonder how I can partial Form and add class that handle in specific event.
For example, on Esc Key the form will close, but for all kind of forms in my project.
So, I thought to add partial class on Form and capture the keys event and if is ESC do Close action on current form.

Any idea ?
I know that i can create a new Form of mine and inherit, but I look for somthing more elegant.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jan-15 4:01am    
Which Form? Is it System.Windows.Forms.Form?
Do you run it as a modal form?
—SA
Matrix2324 25-Jan-15 4:20am    
There is a main form(System.Windows.Forms.Form) that from him calld to sub forms.
I wont that when the key Esc happen the current sub form closed.
But I wont to write the code only in the main form.
I tried to use Wm_Message but unsuccessfully.
Sergey Alexandrovich Kryukov 25-Jan-15 5:21am    
Of course. Now, modal or not? What is "sub-form"? Are you using MDI (better don't)?
If you implement something you need in one form, put it to base form class and inherit it.
—SA
Matrix2324 25-Jan-15 7:04am    
this model, I mean there is main model and from him open a new model form.
I want to mange from the main model on specific event thats happen in another model, like "Esc" key or catch and handle when the enable button change.
Sergey Alexandrovich Kryukov 25-Jan-15 12:13pm    
Model or modal?!
—SA

I can think of 2 ways.
  1. Bind every form to a common event handler.
  2. Catch the key down event with an external event using windows api such as SetWindowsHookEx.
    This will catch every key down in the application.
    To my experience, this method is buggy and unreliable.
 
Share this answer
 
Comments
Matrix2324 25-Jan-15 4:21am    
I think use Wm_Message but unsuccessfully.
Sergey Alexandrovich Kryukov 25-Jan-15 5:34am    
Don't take this advice seriously. :-)
—SA
If the form is modal, it's much better to have "Cancel" and/or "OK" ("Apply", "Accept", "Done") buttons used to finish modal state. Then you can handle Cancel (and/or Enter) by setting the properties CancelButton and/or AcceptButton of these buttons:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.cancelbutton(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton(v=vs.110).aspx[^].

If the form is not modal, closing it by Escape would be a really, really bad idea. But of course you should be able to handle form keyboard event. The trick here is: the focus is usually taken by some other control on the form, so you should do something about it. This is done by setting the form property KeyPreview. Then you can handle the form event PreviewKeyDown or override the method OnPreviewKeyDown:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpreviewkeydown(v=vs.110).aspx[^].

You mentioned that you don't want to do something for all forms. Of course. You should not repeat any code in different form classes; instead, always inherit some base form class with common functionality.

—SA
 
Share this answer
 
Comments
Matrix2324 25-Jan-15 7:28am    
this is not ehat i looking for - i have model.
I want to mange from the main model on specific event thats happen in another model, like "Esc" key or catch and handle when the enable button change.
Sergey Alexandrovich Kryukov 25-Jan-15 12:13pm    
Model or modal. I explained for all cases.
—SA

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