Minimise form when the close [X] button is clicked

J

John O'Neill

Hi

Instead of closing the form when the close button [X] is clicked, I want the
form to minimise. Does anyone know how to do this (using C#)?

Thanks for any help

John
 
F

François J. Miton

Hi John,

You can hook the "closing" event of the form and minimaze it (and cancel the
event)

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

this.WindowState = FormWindowState.Minimized;

e.Cancel=true;

}

HTH

François J Miton
 
J

John O'Neill

Thanks for your suggestion François, but thats not quite what I was hoping
for. When my application is running and I choose to shut-down my computer,
my application just gets minimised and stays running and my computer does
not shutdown (because the Closing event is cancelled). What I think I need
to know is how can I tell if the user has clicked on the [X] button so I can
handle that instead of hooking in to the Closing event?

Thanks again

John


François J. Miton said:
Hi John,

You can hook the "closing" event of the form and minimaze it (and cancel the
event)

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

this.WindowState = FormWindowState.Minimized;

e.Cancel=true;

}

HTH

François J Miton



John O'Neill said:
Hi

Instead of closing the form when the close button [X] is clicked, I want the
form to minimise. Does anyone know how to do this (using C#)?

Thanks for any help

John
 
Y

Ying-Shen Yu[MSFT]

Hi John,
Maybe you can try overriding the WndProc and handling the WM_SYSCOMMAND
message, then change the SC_CLOSE command to SC_MINIMIZE before passing it
to base.WndProc method. For more information , you may read the
documentation of the WM_SYSCOMMAND message.

Does it resolve your problem?
Please feel free to reply this thread if you still have problem on it.

Have a nice day.
Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
J

John O'Neill

Hi Mike

Using this method, I still have the same problem in that my machine will not
shut down. It would seem that the Closing event is being raised before the
ApplicationExit event so the flag does not get set in time.

Thanks for your suggestion though :)

John

Mike said:
Perhaps you can hook up ApplicationExit event of Application object:

// Handle the ApplicationExit event to know when the application is exiting.
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

private void OnApplicationExit(object sender, EventArgs e)
{
// Set a flag to indicate that you are exiting.
}

Then in the closing event, you can check for the flag and allow the event to proceed.

Best,

--Mike
John O'Neill said:
Thanks for your suggestion François, but thats not quite what I was hoping
for. When my application is running and I choose to shut-down my computer,
my application just gets minimised and stays running and my computer does
not shutdown (because the Closing event is cancelled). What I think I need
to know is how can I tell if the user has clicked on the [X] button so I can
handle that instead of hooking in to the Closing event?

Thanks again

John


François J. Miton said:
Hi John,

You can hook the "closing" event of the form and minimaze it (and
cancel
the
event)

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

this.WindowState = FormWindowState.Minimized;

e.Cancel=true;

}

HTH

François J Miton



Hi

Instead of closing the form when the close button [X] is clicked, I want
the
form to minimise. Does anyone know how to do this (using C#)?

Thanks for any help

John
 

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