[RESOLVED] [2005] New, Open, Save, Save As and Exit
Hi!!
In programs like Notepad, Word, Excel, PowerPoint, etc. Not considering the fact that some of these programs support multiple opened documents at the same time, they all share similarities in "new", "open", "save", "save as" and "exit" functions/subs, let me explain:
If you click in "new":
a)There is a document opened, the program asks if you wanna save the current document. This only happens if the current document is different from the last time it was saved (if it was never saved, then its "different"). And you are given three options:
"Yes" - he saves the file, then resets
"No" - just resets
"Cancel" - doesnt do anything
There is no document opened, he "resets" all the configurations (font size, type, etc), well makes a new document
If you click "open":
There is a document opened, happens a) again:
"Yes" - he saves the document, then opens your file
"No" - just opens your file
"Cancel" - doesnt do anything
Theres no opened document, he opens your file.
If you click "Save":
You have never saved, neither you are editing a document opened in the program. That means you opened the program and start to create the document - he goes to "save as"
You opened the document with the program or you already "made" a "save as", he saves your document in the file where you first opened it.
If you click "Save as" - you just save the document in the path chosen by you.
If you click "Exit"
There is a document opened, happens a) again:
"Yes" - he saves the document, then exits program
"No" - just exits program
"Cancel" - doesnt do anything
Theres no opened document, he exists program
Well, so I can achieve this I use a openfiledialog, savefiledialog and string that hold a path to a file (the opened or the save as file).
When u click save and it has to save your document in the file where you first opened it:
I delete the file where you first opened it.
Create a new file, with the same name and in the same path (because i use the string with the path value), this file "was" the "values" of the current document.
And to detect either it has to ask if you wanna save or not (in the a) case):
I create a temporary file with the "values of the current document
I compare if that file and the last saved document (the one "in" string path) are equal, if they are different it asks.
My question inst there any template to make this, maybe in a more sophisticated and efficient way?
Last edited by Lasering; Feb 26th, 2008 at 04:25 PM.
Albert Einstein:
"Imagination is more important than knowledge."
"Everything should be made as simple as possible, but not simpler."
"Great spirits have often encountered violent opposition from weak minds."
Albert Einstein:
"Imagination is more important than knowledge."
"Everything should be made as simple as possible, but not simpler."
"Great spirits have often encountered violent opposition from weak minds."
you should more than likely have some kind of document class, with getter and setter methods for isSaved()
that would be a lot easier,
then you could just keep track multiple docs at once
So on close you could just get that objects saved variable and then save or not save before you close.
And save should only ask you the path the first time, after that, it just overwrites. I don't think you have to delete the old file, I think it will get overwritten automatically.
You could test the filenames in that directory to the one they type in, if they click save as. Other wise do it automatically (clicked save).
you can make it to where only one document can be shown at once, but now a days with dual monitors and all, wouldn't be very likable.
Justin Fox
You down with OOP? Yeah you know me!
MCAD and MCMICKEYMOUSE (vb.net)
Albert Einstein:
"Imagination is more important than knowledge."
"Everything should be made as simple as possible, but not simpler."
"Great spirits have often encountered violent opposition from weak minds."
You seem to have worked out the basics pretty well, so what is stopping you from coding it?
Just give it a try and show us where you get stuck.
I have written these kind of things alot as I'm writing a code editor similar to Notepad (with extra functions).
The essence of what I did is to do two checks:
1. Is the document saved
2. Has the document changed
The code should look something like this:
For NewFile:
Code:
If DocumentChanged = True Then
'Doc has changed so ask to save first
Show Yes/No dialog asking "Do you want to save?"
If Yes Then
'User wants to save
'Check if the document has been saved before,
'or if it's an "Untitled" file
If DocumentSaved = True Then
'Doc has been saved before, just save and open new file
Save the current file
Open a new file
Else
'Doc is an Untitled file, open savefile dialog, then save and open new file
Save the file As... (open savefile dialog)
Open a new file
End if
Else (If No)
'User does not want to save, just open new file
Open a new file
End if
Else (If DocumentChanged = False)
'Document has not changed since last save, just open new file
Open a new file
End if
For OpenFile, it's very similar:
Code:
If DocumentChanged = True Then
'Doc has changed so ask to save first
Show Yes/No dialog asking "Do you want to save?"
If Yes Then
'User wants to save
'Check if the document has been saved before,
'or if it's an "Untitled" file
If DocumentSaved = True Then
'Doc has been saved before, just save and open file
Save the current file
Show OpenFile dialog
Else
'Doc is an Untitled file, open savefile dialog, then save and open file
Save the file As... (open savefile dialog)
Show OpenFile dialog
End if
Else (If No)
'User does not want to save, just open file
Show OpenFile dialog
End if
Else (If DocumentChanged = False)
'Document has not changed since last save, just open file
Show OpenFile dialog
End if
For SaveFile:
Code:
If DocumentSaved = True Then
Save current file (no dialog)
Else
Show Savefile dialog (Save file As...)
End if
Finally, for Save File As... it's just showing the save file dialog.
Note, the little lines like "Open new file" and "Save current file" should be functions that do the corresponding actions.
An "Open New File" function for example should clear the text, set the filename to Untitled, update DocumentSaved and DocumentChanged variables and possibly update the apps title to reflect the current open file.
Now, the only thing left to figure out for you is when to update DocumentChanged and DocumentSaved variables. I'll give you some hints, both variables should be updated after the user either opens a new file, an existing file or saves a file.
The DocumentChanged variable should also be updated when the text changes.
I'm sure you can figure it out now!
If you do it correctly the app should behave exactly the same as Notepad does.
EDIT
One last thing, you will need to check for DocumentChanged and DocumentSaved variables when the user quits the application or closes the file.
You seem to have that figured out aswell.
Last edited by NickThissen; Feb 27th, 2008 at 05:57 PM.
Here, I had a bit of time and quickly wrote up a Notepad Example program that has all the functionality you need for New/Open file/Save file/Save file As/Exit.
It behaves exactly the same as Notepad (except maybe the text in the MsgBox) and also adds a * to the form title when the doc has changed (not in Notepad but you see this in many programs).
Albert Einstein:
"Imagination is more important than knowledge."
"Everything should be made as simple as possible, but not simpler."
"Great spirits have often encountered violent opposition from weak minds."