Nadeem
Nadeem

Reputation: 665

How to show a message on window close button in wpf?

I want to show a message when user click on close button that ARE YOU WANT TO CLOSE?

Upvotes: 0

Views: 2804

Answers (3)

Jeff Mercado
Jeff Mercado

Reputation: 134891

Just add a handler to the Closing event and show a message box with your message. Cancel the event depending on which option the user chooses.

In C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
    }

    void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (MessageBox.Show("ARE YOU WANT TO CLOSE?", "CLOSING", MessageBoxButton.YesNo) == MessageBoxResult.No)
        {
            e.Cancel = true;
        }
    }
}

Upvotes: 5

Rui
Rui

Reputation: 386

I dont know if you are using vb or c# but on Vb you just need to go on your application.xaml.vb window using this code:

Protected Overrides Sub Finalize()
msgBox("Closing")
End Sub

and then every time you press the close button it will show a messagebox.

Upvotes: 0

blindmeis
blindmeis

Reputation: 22445

subscribe the window closing event and put your messagebox into it.

with e.Cancel=true you can cancel the window-close-action.

Upvotes: 1

Related Questions