【问题标题】:Performing some actions before closing winform application in c# [duplicate]在c#中关闭winform应用程序之前执行一些操作[重复]
【发布时间】:2012-11-22 10:39:43
【问题描述】:

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

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

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

查看表单关闭事件

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)
{
}

【讨论】:

【解决方案2】:

查看表单事件处理程序的 FormOnClosed 事件 示例 c# 代码 私人无效formname_FormClosed(对象发送者,FormClosedEventArgs e) { //urcode }

【讨论】:

    【解决方案3】:

    您可以处理 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" );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-01
      • 2010-10-26
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多