Saeq
Saeq

Reputation: 289

Not showing new form if one already exists

I have a button that brings up another form, but the way I do it

    hozzaadasForm HozzaadasForm;

    private void hozzaadButton_Click(object sender, EventArgs e)
    {
        HozzaadasForm = new hozzaadasForm();
        HozzaadasForm.Show();
    }

Opens a new form, everytime I click the button, I don't really want that, but if I do it like this

    hozzaadasForm HozzaadasForm = new hozzaadasForm();

    private void hozzaadButton_Click(object sender, EventArgs e)
    {
        HozzaadasForm.Show();
    }

Once I close it, I can't reopen it. (ObjectDisposedException was unhandled). What can I do so it doesn't open a new one if one is already open, but I can open one, once I close it?

Upvotes: 0

Views: 142

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

In HozzaadasForm, subscribe to the Closing event, then cancel the close and hide the form instead:

private void HozzaadasForm_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;

    this.Hide();
}

Upvotes: 1

d3dave
d3dave

Reputation: 1381

When you close the form, instead of actually closing it, you can call Hide().

Upvotes: 2

Related Questions