Almo
Almo

Reputation: 15861

FormClosing not called when clicking Big Red X in the corner

I found this:

Button with an X at the upper-right corner of the form, how to catch this event @ C#

Which says I should use the FormClosing event to find out when the window is closing because of a click on the X.

But my event code never gets called:

private void MainWin_FormClosing(Object sender, FormClosingEventArgs e)
{
    m_closeThread = true;
    Application.Exit();
}

I must be missing something basic, but I don't know what.

Upvotes: 5

Views: 8352

Answers (2)

Daniel Peñalba
Daniel Peñalba

Reputation: 31857

Make sure that you're correctly subscribing to the FormClosing event.

You must have on your MainWin dialog (tipically in the constructor), something like this:

this.FormClosing += new FormClosingEventHandler(MainWin_FormClosing);

Hope it helps.

Upvotes: 1

Vladimir
Vladimir

Reputation: 3689

You must either subscribe to the event like:

this.FormClosing += this.MainWin_FormClosing;

in the form's constructor (or somewhere), or use:

override void OnFormClosing(FormClosingEventArgs e)
{
    m_closeThread = true;
    Application.Exit();
}

Upvotes: 9

Related Questions