Click here to Skip to main content
15,925,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
helo every one.
i want in c# way to do some action befor form closing by user
(X)buttom ,
becuase i need to save information from the form before user colse this form .
i know i i change form style to none (x)buttom will be disappeare
but i don't want this way .

thanks
Posted

Subscribe to the FormClosing[^] event, just like the title of your post says.
 
Share this answer
 
Well, my solution to this will be :

In FormClosing event, I'll get the 'e.CloseReason' and if it is 'CloseReason.UserClosing' which means user pressed 'X' button on top left corner of the form, I'll do my desired actions, and then, if I want, I'll let the form close, and if dont want to, I can prevent it from closing by setting 'e.Cancel' true.

Here is the code :


private void form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
//Do what you want
//e.Cancel = true; //-> this will prevent form closing.
}
}

-----------------------------------
Regards

H.Maadani
 
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