I'm writing a Windows Forms Application in C# that uses only one form. When I want to exit and close the application, I add the code
private void Defeat()
{
MessageBox.Show("Goodbye");
this.Close();
}
to the class Form1 : Form
, which is the form class that was automatically created by Visual Studio. But when this code runs, I get the following message:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
A picture of the message:
What is the problem?
How should I exit my application?
Exit() Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
You first need to quote your string so the message box knows what to do, and then you should exit your application by telling the application context to exit.
private void Defeat()
{
MessageBox.Show("Goodbye");
Application.Exit();
}
If you want to close the application, please try this:
DialogResult dialog = new DialogResult();
dialog = MessageBox.Show("Do you want to close?", "Alert!", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
System.Environment.Exit(1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With