2016-11-24 6 views
0

終了オプションを持つメニューからウィンドウを閉じることによってアプリケーションを閉じたいと思います。両方ともプロンプトメッセージを追加しました。 しかし、私はあなたがExitボタンを押すか、あなたはALT + F4でアプリケーションを閉じるか、右アップX (cross)が押されるたび、それはShellWindow_Closing終了メニューと終了イベントを使用してアプリケーションをシャットダウン

public partial class SWindow : Window 
{ 
    public SWindow() 
    { 
      this.Closing += ShellWindow_Closing; 
    } 

    private void ShellWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
     if (MessageBox.Show("Any running migration task will be aborted. \nAre you sure you want to exit the application ?", 
      "Exit Proventeq Migration Acclerator", 
      MessageBoxButton.YesNo, 
      MessageBoxImage.Question) == MessageBoxResult.No) 
      e.Cancel = true; 
    } 
} 

private void Exit_Click(object sender, RoutedEventArgs e) 
{ 
    if (MessageBox.Show("Any running migration task will be aborted. \nAre you sure you want to exit the application ?", "Exit Proventeq Migration Acclerator", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) 
     e.Handled = true; 
} 
+1

質問の書式を設定してください。 – FCin

+0

[X]ボタンをクリックしたときの確認の依頼](0120-998-501) –

+0

終了メニューからハンドラセットe.Handled = true;他のハンドラを呼び出すべきではない – adminSoftDK

答えて

0

に行くSystem.Windows.Application.Current.Shutdown();後なぜならExit_Clickイベントにプロンプ​​トメッセージを2回与え、その後、出口を呼び出された場合それは常にあなたのアプリの閉鎖に来る。

簡単な方法は、BtnMenuExit_ClickをAppクロージャとして処理することです。その後、他の動作 - Windowのイベントは、メッセージボックスの結果に基づいてキャンセルに近づきます。

public partial class SWindow : Window 
{ 
    public SWindow() 
    { 
     this.Closing += ShellWindow_Closing; 
    } 

    private void ShellWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
     if (MessageBox.Show("Any running migration task will be aborted.\nAre you sure you want to exit the application ?", "Exit Proventeq Migration Acclerator", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) 
     { 
      e.Cancel = true; 
     } 
    } 

    private void BtnMenuExit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 

//designer part 
public partial class SWindow 
{ 
    //other code 
    private void InitializeComponent() 
    { 
     Button BtnMenuExit = new Button(); 
     BtnMenuExit.Click += new EventHandler(BtnMenuExit_Click); 
     //other styles etc.. 
    } 
    //other code 
} 
+1

Ctrl-F4はアプリケーションを閉じず、MDIアプリケーション内のMDI-Childフォームのみを閉じることができます。あなたが必要とするアプリケーションを閉じるにはAlt-F4 – GuidoG

+0

ありがとう、私はタイプミスです。 – Tatranskymedved

関連する問題