VB.NET - save and save as options in notepad project

Asked By jenn on 18-May-10 01:04 PM
good morning all :)

wondering this:
when you open Microsoft word, under file you have an option to save...and an option to save as.

I am creating a notepad and would also like to have both options in it as well.

seems to me though that they both work the same way. LOL when I open my word and type something and then hit save..i get a dialog box where I can type in the name I want to give it..and the same thing happens in save as..

so really I dont understand these options..only that people are use to seeing both options!

here is my code I am using for save:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Try
            Dim dlg As SaveFileDialog = New SaveFileDialog
            dlg.Title = "Save"
            dlg.Filter = "Rich Text Files (*.rtf)|*.rtf"
            If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
            End If
        Catch ex As Exception : End Try
  
    End Sub


So theoretically cant I just add a save as option in my menu and paste this code into it as well??? or do I just not get it LOL
Phivos Stylianides replied to jenn on 18-May-10 01:17 PM
You already have the code for save as. Now save implies that you have already opened a file before so when the user hits the save button, that file will be overwritten with the new contents. All you need to do is keep the browsed file path in a string and when the user clicks save, update the previous file.
Santhosh N replied to jenn on 19-May-10 12:11 AM
Save and Save As slightly work differently even though from a certain standpoint they resemble to be same
Save overwrites within the same file name or ask for a new filename if a new one, where as Save As always asks for a new filename it saves in new one...

You can check here for different implementations of notepad saving in .net
http://www.csharpfriends.com/articles/getarticle.aspx?articleid=84
Super Man replied to jenn on 19-May-10 11:52 AM

save as ...creates the new file in the disk and saves the data

save ....saves the data to disk in opened file,

you have to maintain one string variable like "fname"

for that on both button , check if fname is null, then open SaveFileDialog.

and if fname is not null, then 

on save click, save the data to filename which is stored in fname.

and

on save as , if fname exists, then ask user to save unsaved data

                 and  then open SaveFileDialog.

jenn replied to Phivos Stylianides on 20-May-10 05:33 PM
alright, so what your telling me is that I already have the save as done, and what I am looking for is the save.

I do understand the differences between the two of them...save and save as. I switched what I thought was for the save and placed it under the save as..

I dont understand what you mean by

"All you need to do is keep the browsed file path in a string and when the user clicks save, update the previous file"

how do you do this?

LOL..you know that I am so very new at this as you have helped me in the past with datagridview LOL

which by the way is now all solved :)

Phivos Stylianides replied to jenn on 20-May-10 05:47 PM
Ok, say you open a file dialog and the user selects a file. Then you use the file path chosen by the user to open it and display the file contents in your editor. Now, that you know the file path(assuming you previously stored it in a string accessible to all methods), when the user makes any changes to the text and clicks the save button, you overwrite the file in that path with the new contents. If however the user clicks save as you display another dialog box for the user to select where to save the modified text, because obviously he/she does not want to overwrite the original. Just like Word and many others work. Does it make sense?
jenn replied to Phivos Stylianides on 21-May-10 03:19 PM
yes that makes sense :)

so all in all the save and the save as use the same code, except that on the save option there is no dialog box...the dialog box is only on the save as option.
Phivos Stylianides replied to jenn on 21-May-10 04:10 PM
Exactly!