Click here to Skip to main content
15,441,280 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i created a simple windows application in VS 2008.i don't want the application to close when the user presses the exit button[cross button on right hand top corner].Is it possible?? if yes please tell me the coding in detail.
Posted

First subscribe to the Form's Closing event. After that in the event handler you can cancel the close:
C#
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{     
     // Cancel the close action
     e.Cancel = true;
}


But this code alone will prevent you from ever closing the application! In order to provide a way of closing the form, you can use a flag to determine whether or not the closing event should be cancelled.

I hope this helps. :)
 
Share this answer
 
If you aren't worried about losing the other control buttons (minimise/restore/maximise) you could set the Forms ControlBox property to false, then there wouldn't be an exit button to press.

this.ControlBox = false;


Probably best done in the forms constructor after InitializeComponent() is called. Either that or done in the Designer using the Properties window.
 
Share this answer
 

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