How do I close a form after I open another form

<>

This question already has an answer here:

Answers


If the parent of Button1 is Form1 you can use :

var formToBeClosed = ((sender as Button).Parent) as Form;
formToBeClosed.Close();

Just switch some lines:

private void button1_Click(object sender, EventArgs e)
{
    Form2 AFormInstance = new Form2();
    AFormInstance.Show();
    this.Close();
}

Edit: On further thought: aren't you accidentally closing your main form?

Change the default code of program.cs to:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form1 = new Form1();
        form1.Show();
        Application.Run();
    }

To close the application use:

Application.Exit();

Need Your Help

How can I map database table and field names back into their EF Entity object and property names?

sql-server entity-framework mapping

I have a database table which records all field changes in all database tables which are mapped to Entity Framework Entities (via SQL Server triggers).