2

I want to perform some actions after the user clicks x button but before the program exits.

Is it possible ? If yes then how can i do it ?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • 1
    http://stackoverflow.com/questions/4851156/window-close-events-in-a-winforms-application – jrb Nov 22 '12 at 10:42

3 Answers3

6

Look at the form closing event

Code example VB.Net:

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

End Sub

and c#

private void Form1_FormClosing(System.Object sender, System.Windows.Forms.FormClosingEventArgs e)
{
}
Ric
  • 12,855
  • 3
  • 30
  • 36
  • Msdn => http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.100).aspx – MarcD Nov 22 '12 at 10:45
0

Look FormOnClosed Event of form event handlers EXAMPLE c# CODE private void formname_FormClosed(object sender, FormClosedEventArgs e) { //urcode }

0

You can handle event FormClosing. Here it is documented: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx

Code sample:

 private void Form1_FormClosing(Object sender, FormClosingEventArgs e) 
 {

    System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
    messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason );
    messageBoxCS.AppendLine();
    messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel );
    messageBoxCS.AppendLine();
    MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event" );
}
Sarrus
  • 586
  • 7
  • 21