In Visual C#, how can I detect if the user clicks the X button to close the program? I want to ask the user if they'd like to perform a certain action before exiting. I have an exit button in my program itself, and I know I can code it as follows:
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result;
if (logfiletextbox.Text != "")
{
result = MessageBox.Show("Would you like to save the current logfile?", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result == DialogResult.Yes)
{
savelog.PerformClick();
}
}
Environment.Exit(0); //exit program
}
But how can I do this for the X button that is already built into the program?