Want Form.CLose to Hide()

G

Guest

Hi;

I have a modeless form (not modal). I want when the user clicks on the X
that it hides instead of closes. I was going to handle this in the Closing
event but I also need to be able to close the window at some time - so that
won't work.

So, any suggestions?

Also, is there a call I can make on a Form to see if it has been closed?
 
J

Jeffrey Tan[MSFT]

Hi dave,

Thanks for your post.

Based on my understanding, you want to change the default behavior of "x"
button of the form, however, still can have a way to close the form
programmatically.

Normally, when we clicked the "x" button of the form, or chose "close" menu
item from the left-top system menu on the form(Alt+F4), the Form will
receive a WM_SYSCOMMAND message with SC_CLOSE in wParam. So we can handle
this situation and disable the default processing of this behavior. Sample
code like this:

private const int WM_SYSCOMMAND=0x0112;
private const int SC_CLOSE=0xF060;
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_SYSCOMMAND)
{
if(m.WParam==(IntPtr)SC_CLOSE)
{
this.Hide();
m.LParam=IntPtr.Zero;
return;
}
}
base.WndProc (ref m);
}

private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
Note: we can still use Form.Close method to close the form
programmatically.(It internally posts WM_CLOSE message to the form wndproc)
=================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top