PJW
PJW

Reputation: 5417

Opening a new form, and closing the form that called it

I have a button on the first form, that when clicked opens a second form and closes the first. However, no mater what I do 'both' forms close.

This is the button on the first form:

private void btnReports_Click(object sender, EventArgs e)
    {
        Form f2 = new frmForm2();
        f2.Show();            
    }

And this is the code in the Load event of the second form

private void frmReports_Load(object sender, EventArgs e)
    {
        Application.OpenForms["frmForm1"].Close();
    }

I also tried

private void btnReports_Click(object sender, EventArgs e)
    {
        Form f2 = new frmForm2();
        f2.Show();   
        this.Close();
    }

Upvotes: 0

Views: 264

Answers (2)

Lukazoid
Lukazoid

Reputation: 19426

Take a look at the following Application.Run(ApplicationContext) method. There is an example on how to set up your application so that it will only exit when the last form is closed, not when the main form is closed.

Upvotes: 0

RvdK
RvdK

Reputation: 19800

You probably have this as startup:

Application.Run(new frmForm1());

If frmForm1 is closed then the application stops. You should hide the form using frmForm1.hide();

Upvotes: 3

Related Questions