i want a message box to ask for unsaved data on Form closing Event.
if the user choose yes then save the data in text file and exit application.
if the user choose no exit application without saving.
i have tried the following code.But it does not close the application and make message box appear again and again.
public void SaveMyFile()
{
//// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog saveFile1 = new SaveFileDialog();
//// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.txt";
saveFile1.Filter = "Info Changed Data (*.txt)|*.txt";
//// Determine if the user selected a file name from the saveFileDialog.
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
//// Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Do You Want To Save Your Data", "CodeJuggler", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
SaveMyFile();
this.Close();
}
else if (dialogResult == DialogResult.No)
{
this.Close();
}
}