2009-10-26 2 views
25

Windowsフォームの閉じ方を検出するにはどうすればよいですか?たとえば、ユーザーがフォームを閉じるボタンをクリックしたかどうか、またはユーザーが右上の「X」をクリックしたかどうかを調べるにはどうすればよいですか?ありがとうございました。フォームクローズ理由の検出

更新:

ボタンがApplication.Exit()メソッドを呼び出すことに言及し忘れました。

答えて

35

で、ドミトリMatveevは既に解決策を述べた種類のCloseReasonのプロパティがあるべき引数FormClosingEventArgs。しかし、ドミトリーも言ったように、これはあなたのボタンと右上隅のXとの間に何の違いもありません。

これら2つのオプションを区別するために、フォームにブール値のプロパティExitButtonClickedを追加して、Application.Exit()を呼び出す直前にボタンクリックイベントでtrueに設定できます。

FormClosingイベント内でこのプロパティを尋ね、ケース内のこれらの2つのオプションを区別することができます。UserClosing

例:ネイトによって記述

public bool UserClosing { get; set; } 

    public FormMain() 
    { 
     InitializeComponent(); 

     UserClosing = false; 
     this.buttonExit.Click += new EventHandler(buttonExit_Click); 
     this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); 
    } 

    void buttonExit_Click(object sender, EventArgs e) 
    { 
     UserClosing = true; 
     this.Close(); 
    } 

    void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     switch (e.CloseReason) 
     { 
      case CloseReason.ApplicationExitCall: 
       break; 
      case CloseReason.FormOwnerClosing: 
       break; 
      case CloseReason.MdiFormClosing: 
       break; 
      case CloseReason.None: 
       break; 
      case CloseReason.TaskManagerClosing: 
       break; 
      case CloseReason.UserClosing: 
       if (UserClosing) 
       { 
        //what should happen if the user hitted the button? 
       } 
       else 
       { 
        //what should happen if the user hitted the x in the upper right corner? 
       } 
       break; 
      case CloseReason.WindowsShutDown: 
       break; 
      default: 
       break; 
     } 

     // Set it back to false, just for the case e.Cancel was set to true 
     // and the closing was aborted. 
     UserClosing = false; 
    } 
+2

オリバー、あなたは今、重大な間違いがあります。閉じる理由がCloseReason.ApplicationExitCallであり、if(UserClosing)条件が役に立たないときにクリックハンドラからApplication.Exitを呼び出すと、 Click Handlerのthis.Close()メソッドをUserClosingのswitch caseと一致するように呼び出すか、if文をswitch文の外側に移動する必要があります。 – okutane

+0

Form1_FormClosingでフォームが閉じられない場合は、「終了しましたか?」というメッセージが表示されます。質問 - 何かがUserClosingフラグ変数をfalseに設定する必要があるか、または次に誤って真になる可能性があります。 –

4

FormClosingEventArgsのCloseReasonプロパティをFormClosingイベントハンドラでチェックして、いくつかのケースを確認できます。ただし、このプロパティのみを使用する場合は、あなたが記述したケースは区別できません。 「閉じる」ボタンのクリックイベントハンドラにいくつかの追加コードを記述して、これらのケースを区別するためにFormClosingイベントハンドラでチェックされる情報を保存する必要があります。あなたがイベントに送信してもFormClosing、にリスナーを追加する必要が

0

は、これらの値の1 bashmohandesとして

// Summary: 
//  Specifies the reason that a form was closed. 
public enum CloseReason 
{ 
    // Summary: 
    //  The cause of the closure was not defined or could not be determined. 
    None = 0, 
    // 
    // Summary: 
    //  The operating system is closing all applications before shutting down. 
    WindowsShutDown = 1, 
    // 
    // Summary: 
    //  The parent form of this multiple document interface (MDI) form is closing. 
    MdiFormClosing = 2, 
    // 
    // Summary: 
    //  The user is closing the form through the user interface (UI), for example 
    //  by clicking the Close button on the form window, selecting Close from the 
    //  window's control menu, or pressing ALT+F4. 
    UserClosing = 3, 
    // 
    // Summary: 
    //  The Microsoft Windows Task Manager is closing the application. 
    TaskManagerClosing = 4, 
    // 
    // Summary: 
    //  The owner form is closing. 
    FormOwnerClosing = 5, 
    // 
    // Summary: 
    //  The System.Windows.Forms.Application.Exit() method of the System.Windows.Forms.Application 
    //  class was invoked. 
    ApplicationExitCall = 6, 
} 
+1

いずれの場合もCloseReason.UserClosingになりますので、どのようにこのプロパティの使用は、彼を助けることができますか? – okutane

関連する問題