code_Finder
code_Finder

Reputation: 305

Best Way to Close a Win Form

I need to close a form on a button click event.Here in my example I am hiding the form.Think this is not a good way.When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.

private void buttonClose_Click(object sender, EventArgs e)
{
    this.Close(); //closing frmCalender 
}

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

Give me the best way to close a C# Windows Form.

Upvotes: 1

Views: 97

Answers (2)

yaakov
yaakov

Reputation: 5851

If you want to close a form, call .Close().

When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.

When you close the form, I assume you have no references to it. If so, you can create a new copy of your form via the constructor (var form = new MyForm();).

Otherwise, after closing the form, I believe you should be able to call .Show() on it again, as long as something still has a reference to your form.

Upvotes: 3

platon
platon

Reputation: 5340

I think, the best approach would be:

private void buttonClose_Click(object sender, EventArgs e)
{
    this.Hide(); 
}

Upvotes: -1

Related Questions