C# – Hide form instead of closing when close button clicked

cnetwinforms

When a user clicks the X button on a form, how can I hide it instead of closing it?

I have tried this.hide() in FormClosing but it still closes the form.

Best Answer

Like so:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing) 
    {
        e.Cancel = true;
        Hide();
    }
}

(via Tim Huffman)