views:

349

answers:

2

Hi, I'm filtering the messages that come to a form with PreFilterMessage like this:

print("code sample");

 public bool PreFilterMessage(ref Message m) 
 {
     if (m.Msg == WM_KEYDOWN && (int)m.WParam == VK_ESCAPE)
     {
         this.Close();
         return true;
     }
     return false;
 }

print("code sample");

but the matter is that form closes only for the first time. After reopening a form it won't close anymore by pressing ESC.

How can I accomplish this?

Thanks

+1  A: 

I don't know if this fits with what you are doing. I usually set Form.CancelButton to the close or cancel button on my form, and it will automatically call the button OnClick when the user hits Esc on the keyboard.

Hallgrim
A: 

According to MSDN

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

If you Indeed shown you form using ShowDialog(), then calling Close() doesn't dispose off your form. You could still be able to "reopen" it later, and probably taht's what you're doing. I'm suspecting that you might have distrupted PreFilterMessage() when you first close it. Have you check if the message loop is still working? Or you should actually do this.Visible = false; or Control.Hide,

When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the Control..::.Hide method.

since you need to "reopen" it later. If you are actually expecting the form dispose off, and show a new instance later, then manually call dispose on it, after closing

faulty