【发布时间】:2014-07-21 09:18:30
【问题描述】:
我想要一个消息框来询问有关表单关闭事件的未保存数据。
如果用户选择是,则将数据保存在文本文件中并退出应用程序。
如果用户在不保存的情况下选择不退出应用程序。
我尝试了下面的代码。但是它并没有关闭应用程序并让消息框一次又一次地出现。
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();
}
}
【问题讨论】: