Text Editor, Ask to save

reconrey

Newcomer
Joined
Feb 18, 2010
Messages
7
Well im wondering how to make my text editor ask to save the changes before closing, what i mean is if you changed the document it will have a message box if it has been saved after changens, i know i can do:
Visual Basic:
Dim SaveSts as String = "Saved"
'Under TextBox1_TextChanged:
SaveSts = "Unsaved"
'Under Savebtn_Click:
SaveSts = "Saved"
'Under Form1_FormClosing:
'use e.cancel  = true and a msgbox
Is there a better way of doing this?
 
Last edited by a moderator:
Other than the fact I would use a boolean rather than a string to track if the file has been saved or not your method looks fine to me.

I am assuming you are going to use the MessageBox to prompt them to save / cancel and then set e.Handled accordingly.
 
Personally, my preferred method is to store a copy of the original document (in memory) and compare it to the current version of the document. You might find yourself in a situation where the document might be larger that something you would like to keep in RAM. In that case you could consider storing a hash of the original document and comparing it to a hash of the current document.

PD is right, though. In this simple case, your method should work fine. In some situations there may be many places in the UI where the document can be modified in different ways. Being able to compare to the saved version of the document seems to cover all bases.
 
A boolean is a simple true or false option and in your situation a document has either been modified since the last save or not - to me a boolean seems the natural choice to represent this quality.

A string would require more memory than a boolean and is susceptible to spelling mistakes and typos with no compiler validation while a boolean prevents this kind of issue.
 
ok...i think i will use a boolean then :D it would go sumthin like this right?

Code:
Private Declare Function Saved as Boolean = True
 
Back
Top