HoBa
HoBa

Reputation: 3604

How to override onclose event on WPF?

I'm trying to override the onclose event on WPF, this is my code so far:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
       base.OnClosing(e);
       e.Cancel = true;
       //do my stuff before closing
}

It executes the code but the application never closes. Any ideas how to fix this?

Upvotes: 10

Views: 26629

Answers (2)

Rick Sladkey
Rick Sladkey

Reputation: 34250

You are asking it not to close by setting e.Cancel = true. Just don't do that.

Upvotes: 6

Bala R
Bala R

Reputation: 109027

The application never closes because you are setting e.Cancel to true.

Try

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
       //do my stuff before closing

       base.OnClosing(e);       
}

Upvotes: 19

Related Questions