在c#中关闭winform应用程序之前执行一些操作

时间:2012-11-22 10:39:43

标签: c# .net winforms

我想在用户点击x按钮之后但在程序退出之前执行一些操作。

有可能吗?如果是,那我该怎么办呢?

3 个答案:

答案 0 :(得分:5)

查看表单结束事件

代码示例VB.Net:

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

End Sub

和c#

private void Form1_FormClosing(System.Object sender, System.Windows.Forms.FormClosingEventArgs e)
{
}

答案 1 :(得分:0)

查看表单事件处理程序的FormOnClosed事件 示例c#代码  private void formname_FormClosed(object sender,FormClosedEventArgs e)         {                  // urcode         }

答案 2 :(得分:0)

您可以处理事件FormClosing。这里记录了:http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx

代码示例:

 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" );
}