Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made two forms in c#, I want my second form not to be closed by clicking on the cross button. How can I do this?
Posted
Updated 19-Aug-11 0:48am
v3

This will work just fine:
C#
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
 {
     MessageBox.Show("Sorry, you cannot close this form");
     e.Cancel = true;
 }


NOTE:
this should not be the main form as user will not be able to end your application until the manually end the process. I dont see a reason why you dont want a form to be closed, you can confirm if the user really want to close the form or not before deciding whether to cancel the event arg's or not
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-11 21:52pm    
It can be a main form as Application.Exit (in menu, for example) will do the trick.
Also, names like Form2_FormClosing violate (good) Microsoft naming conventions. Yes, this is auto-generated by Microsoft, but nobody says you're allowed to keep such ugly non-compliant names; everything in used should be renamed in some semantic way. Also, good to show anonymous delegate syntax. So, I voted 4, not 5.
--SA
Oludayo Alli 22-Aug-11 1:43am    
Thanks SA, I'm well aware of the naming convention & I expect that he has his own FormClosingEventArgs; also If the form is the main form, then he has to look for a way for fire the Application.Exit whenever he wants the user to exit the application. I was unable to paint a picture of why he want to avoid the main form to be closed, apologies if I left a wrong impression before.
Set the form's ControlBox property to false.
 
Share this answer
 
Comments
M Ali Qadir 19-Aug-11 6:59am    
No i want that option of cross button should be there but when i click on it ,it message me that you can't close it.
[no name] 19-Aug-11 7:02am    
Handle the FormClosing event and set the event arg's Cancel property to true.
nagendrathecoder 19-Aug-11 7:44am    
Good answer, my 5
M Ali Qadir 19-Aug-11 7:08am    
yes great that was my task to do.hope you will help in future.thanks
Suresh Suthar 19-Aug-11 7:11am    
Yes he will (thats why we are here). But at least you can accept the answer..
Set the form's ControlBox property to false.
 
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