1
votes

I have two forms .. the form1 is a login form. so if the username and the password typed correctly .. the user will be directed to the form2 and hide form1.. The form2 contain a button "logout" for closing form2 and returning to form1 ..

this is the code for the button logout

  private void logout_Click(object sender, EventArgs e)
    {
        this.Close();
        foreach (Form OpenedForm in Application.OpenForms)
        {
            if (OpenedForm is Form1)
            {
                OpenedForm.Show();
                break;
            }
        }
    }

everything is ok with that .. but the only problem appears when the user clicks the red cross to exit the application directly without logging-out .. in this case the form1 (login) still hidden .. Is there a solution to close form1 when closing form2 from the red cross ? thanks and sorry for my poor english

1
Handle the closing event of form2, refactor out the body of logout_Click to a common function, and have both events call it - add an argument for logout vs. exituser326608
A hidden form will automatically be closed when the application exits. So if closing the main form causes the application to exit, then the hidden login form will also be hidden. Very simple, no need to mess around with handling additional events.Cody Gray

1 Answers

3
votes

You can try to use FormClosing Event.

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    // close the first form...
}

This thread can help you to detect when the red cross was clicked...