arhud
arhud

Reputation: 69

Taskbar Button rightclick: Customized menu in windows forms

I do not want the form to exit when the "close window" option is clicked in the menu that pops up when the taskbar button is right clicked. Instead, I want the application to be minimized to the system tray. How do I change the behavior of the "Close Window"?

Upvotes: 0

Views: 947

Answers (1)

user203570
user203570

Reputation:

Add an override of OnFormClosing and look at the CloseReason of the event arguments parameter. Maybe something like this:

protected override OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        this.Hide();
    }
    else
    {
        this.Close();
    }
}

This way, the user cannot close your form (only hide it), but Windows still can for other reasons (e.g. shutdown).

Upvotes: 1

Related Questions