'close warning keep poping up

There is two forms in my program,The first one is a simple login form that after everything was right loginform hides and the other shows. But the problem is if i try to close the program at loginform and press no then when i press signin button with correct values the closing messagebox shows up again with no reason!!!!

    private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
           DialogResult DR= MessageBox.Show("do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
            if (DR == DialogResult.Yes)
                Application.Exit();
            else if (DR == DialogResult.No)
                e.Cancel=true;
        }
    }
    private void Sign_in_Button_Click(object sender, EventArgs e)
    {
        if (AccountExistStatus(UserNameLogin.Text)==true&&PasswordMatch(PasswordLogin.Text))
        {
            this.Hide();
        }
        else if (UserNameLogin.Text == "Echo" && PasswordLogin.Text == "XLVI")
        {
            this.Hide();
        }
    }


Solution 1:[1]

I think your problem is that you have just hidden that Login form, you have not closed it anywhere. Have a look at the following code which solves your problem. it will close your LoginForm also at the close of the next form

private void Sign_in_Button_Click(object sender, EventArgs e)
{
    if (AccountExistStatus(UserNameLogin.Text)==true&&PasswordMatch(PasswordLogin.Text))
    {
       DisplayForm2();
    }
    else if (UserNameLogin.Text == "Echo" && PasswordLogin.Text == "XLVI")
    {
        DisplayForm2();
    }
}

void DisplayForm2() {
    this.Hide();
    var form2 = new Form2();
    form2.Closed += (s, args) => this.Close(); 
    form2.Show();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pankaj