Windows Develop Bookmark and Share   
 index > Windows Forms General > Save Before Exit?
 

Save Before Exit?

So I have a text editor I'm working on. One of the things I need to add is where when you close the program, if one or more of your files aren't saved, it will ask you if you want to save them before closing, and it gives you the options "Save", "Don't Save", or "Cancel", the last telling the application that "sorry, I didn't mean to close".

Is there a tutorial or something that I could read to learn how to do this?
Joudoki  Wednesday, August 02, 2006 4:34 AM

Before you close your form, you can use the Form.FormClosing event. This way, if you click on the X in the upper-right or you your menu exit, it will still throw this event. Here is a quick event handler that I wrote to illustrate how to keep the application from closing.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (richTextBox1.Rtf != initialRtfString)

{

switch (MessageBox.Show("Do you want to save?", "Save File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation))

{

case DialogResult.Yes:

SaveFile();

e.Cancel = false;

break;

case DialogResult.No:

e.Cancel = false;

break;

case DialogResult.Cancel:

e.Cancel = true;

break;

}

}

}

The MessageBox will show you those options that you requested.

Haynes  Wednesday, August 02, 2006 6:29 AM

Before you close your form, you can use the Form.FormClosing event. This way, if you click on the X in the upper-right or you your menu exit, it will still throw this event. Here is a quick event handler that I wrote to illustrate how to keep the application from closing.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (richTextBox1.Rtf != initialRtfString)

{

switch (MessageBox.Show("Do you want to save?", "Save File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation))

{

case DialogResult.Yes:

SaveFile();

e.Cancel = false;

break;

case DialogResult.No:

e.Cancel = false;

break;

case DialogResult.Cancel:

e.Cancel = true;

break;

}

}

}

The MessageBox will show you those options that you requested.

Haynes  Wednesday, August 02, 2006 6:29 AM
private string _saveLocation;

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
StatusUpdateMessage("Saving Configuration...", 0);
SaveConfig();
StatusComplete();
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAsConfig();
}

private void exitWithoutSavingToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}

private void mnuExitItem_Click(object sender, EventArgs e)
{
if (_saveLocation != null)
{
Close();
}
else
{
SaveAsConfig();
}
}

private void SaveConfig()
{
if (_saveLocation != null)
{
SaveSetting();
}
else
{
SaveAsConfig();
}
}

private void SaveAsConfig()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = _filterDialogText;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
_saveLocation = saveFileDialog1.FileName;
SaveSetting();
}
catch (Exception exception)
{ ; }
finally
{
Cursor = Cursors.Default;
}
}
}
onluong  Wednesday, March 26, 2008 4:56 PM

You can use google to search for other answers

Custom Search

More Threads

• Login.Live.Com --> How to create a login dialog box?
• RichTextBox Control
• listview
• Monthcalendar selection
• OnSizeChanged called twice when changing the property Size on a UserControl
• selecting a listview row
• onkeypress
• How to implement scrolling two richtext boxes at the same time?
• VScrollBar resizing issues when writing a custom Control subclass
• Panels in the StatusStrip