Simple text editor (visual studio 2008) - part 2 - Save as option

R

Rob W

Greetings,

A carry on from my previous post regarding writing a simple text editor. I
have implemented a "Save as" option where a save dialog appears on
conditions:-

* "Save as" option is selected from the file menu

* Saving a document on inital openning of the editor or new file option
(default document name is "document")

My solution for the second criteria checks if the document name is the
default name i.e. "document" andAlso the document has been modified using
the textbox modified property andAlso a boolen variable called initalWrite
is true (Upon form load initalWrite is set to true).

Then during the "save as" operation the boolen variable initialWrite is set
to false.

Another idea was for when the editor is started or a new file is created
store the current date/time in a variable and compare the modified timestamp
of the default document "document" to check if it has been saved within the
session. This solution appears over complicated compared to using a boolean.

As this is for an assignment the code and design will be scruitinised and as
I hope to use these reports to show employees I want to ensure my logic is
sound and always looking for the most cleanest simplest solution to solve a
problem.

Hope I don't sound too pedantic but its for the reasons above I want to
ensure I do my best.

Any input to my current solution using the boolean variable? This works as
required btw.

Thanks

Rob
 
C

Cor Ligthert[MVP]

Rob,

As it is so important for you, then make from your documents a class (a
Type).

That class should have properties like "IsChanged", it also has a method to
save it where you test what the status is from "IsChanged".

As you then open a document is the first thing which you do to create a new
document object and place the text in the Text property of that.
That text property could be very good be the one that you use as the
"ToString" method is called (You overload it) as well as it will be the
default return value.

You can as well make a base document, and special documents just by
inheriting your base document.

Doing it like that is the way it should be done in OOP.
It makes that your programs become very easily maintanable because all
aspects of your documents are in that class.

Cor
 
J

James Hahn

You can't rely on the document name to indicate whether or not the Save As
dialogue should pop up, and in any case it is poor practice. Keep track of
the actual saved document name. If it is blank then the document hasn't
been saved yet. Update it when the Save As dialogue completes successfully.
This will then work correctly even if the user saves the document using the
default name.

Why would you pop up the Save As dialogue on initial opening of the editor?
This seems to imply that you are going to require your user to create the
file for the document before they have entered any text. If this is what
you are doing, then there is no need to pop up the Save As dialogue other
then as a menu option. Of course, if you don't do this then the Save As
dialogue should appear when the user tries to close the editor, which to me
seems the preferable approach.

I can't work out what purpose InitialWrite serves, but I suspect that if you
use a decent test for whether or not the document has been processed through
a Save As dialogue at least once (together with the modified flag, of
course), then it will not be required.
 
R

Rob W

Sorry my initial post wasn't clear.

The default document name is "document" in wordpad and the save as dialog
should appear when save is pressed if this is a new document.

After thinking about InitialWrite isn't needed at all I just used my two
variables:-

Constant DefaultDocument as string = "Document"
ActiveDocument as string.

I then then check if the activename is empty and text has been modified
which does the job.
As if a file has been opened or saved it will have an ActiveDocument name.

Job Done.

I just got a little carried away and didnt spot the obvious. Thanks for all
the tips.

My module covers OOP later on, so I will be doing both procedural and OOP
solutions.

Cheers
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top