Nate Nate - 3 months ago 6
C# Question

End a task if the windows form is also terminated through task manager

I have a login windows form that pops up when I open MS Access. If the user decides to leave, I have a cancel button that closes the application and MS Access:

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}


But I also want to add that if the user terminates the windows form, it will also terminate MS Access.

ie., if windowForm terminated, then Application.Exit

Answer

Add a function to the FormClosing event manually or in the Event Properties of your Form:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

And then write:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}