Code:
private void Form1_FormClosing(object sender, CancelEventArgs e)
{
if (richTextBox1.Modified == true)
{
if (MessageBox.Show("Do you wish to save any changes? All unsaved changes will be lost.", "Test", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string Saved_File = "";
saveFD.InitialDirectory = "Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)";
saveFD.Title = "Save a File";
saveFD.FileName = "";
saveFD.Filter = "Text Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat|HTML Files (*.html)|*.html";
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
Saved_File = saveFD.FileName;
richTextBox1.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
}
richTextBox1.Modified = false; <-- This line is rather pointless now? The application closes if the selects to save the file or not.
}
}
Okay, so I've removed the Tag property and replaced it with the Modified property, which should make the code more efficient because I'm not creating one big string variable. I need to change my main project too now.
Code:
richTextBox1.Tag = richTextBox1.Text;
When I exit the application, but haven't made any changes to a opened file, the message box is still prompted for saving any changes to the file.
I could live with it like this, but it would be nice to know how to monitor changes to a opened file and not the rich textbox like Notepad does.