When a Form_Closed event occurs, there is a phenomenon that it is hidden by pressing Yes or No in the message box.

Asked 1 years ago, Updated 1 years ago, 99 views

if (MessageBox.Show ("Do you want to exit?", "Information", MessageBoxButtons.YesNo) == DialogResult.Yes)
 {
    Application.Exit();
 }

I wrote the code like above. Naturally, when No is called, I thought there would be no event such as hiding the program (call the Hide function) or ending, but it was hidden as if there was a Hide() function.(Remain in process) Is there a way to prevent this?

c# modal visual-studio

2022-09-21 11:38

1 Answers

Prevention is...What does it mean?Event processing when the user clicks the x at the top of the window or clicks the end button of the program.If the DialogResult result is No, skip the exit logic.

You can do the Form Closing process as shown below.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
            varresult = MessageBox.Show ("Are you sure you want to exit the program?"", "Check Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult.No == result) e.Cancel = true; 
}


2022-09-21 11:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.