Lim Jun Wei
Lim Jun Wei

Reputation: 97

Application.Exit() not working properly

I have a separate windows form for user to select background music and it will always stay there unless the application is closed. I prevented the user from closing the music form by using the code:

private void Music_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }

and in my main Program.cs i run a page called login like so:

Application.Run(new Login());

I also have a FormClosed event in all my forms that closes the whole program after pressing the cross

private void Login_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }

However, after i add the code that the user is not able to close the music form, my application is only able to exit by pressing the cross when it is in the login page, which is the start of the program ran by the main page (the application used to be able to exit in all of my forms by pressing the cross)

I want to know if there is any way for me to properly exit my application or a way to make user not able to close the music form without affecting my other form's closing. Thank you

Upvotes: 0

Views: 1437

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205829

The code inside Music_FormClosing is preventing your application from exiting. To get the desired behavior (prevent the user closing the music form), you can utilize the FormClosingEventArgs CloseReason property:

private void Music_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
        e.Cancel = true;
}

Upvotes: 3

Related Questions