Hello world of DaniWeb, I've asked a similar question to this one before but now I'm trying to find an answer to a different problem. You see, I have made a text editing application and, as you might have guessed yourself, I needed to add save features. Well, what I have implemented so far is the ordinary "Save As..." feature which I made by using a common SaveFileDialog. Now it's time for the hard part. I need to add the "Save" function located in many Windows applications (Notepad etc.). This is what I'm working on at this moment but it doesn't seem to work. Please have a quick look:

Public Class Form1
    Dim Saved As Boolean = False

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            ElseIf Saved = True Then
' I don't know what to type here. That's why I'm asking for your help after all! :P
            End If
        End If
    End Sub

As you can see, I've used a nice method to determine whether data was previously saved so the Save function will be called. The thing is that I don't know how to get the save path so I can replace the file that was previously saved with its new version if you know what I mean. Just use Notepad's save function (not "Save As...") and you'll understand what I'm trying to say. I'm looking forward to an easy-to-understand answer. Thank you in advance!

Recommended Answers

All 9 Replies

Its simple:

ElseIf Saved = True Then        
     'This will overwrite it      
     RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
     Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"           
End If

If the file is already saved then path is already there. Just copy the code used in "Saved = false" but only remove the ShowDialog() part.

Hope that helps.

Thanks

It doesn't work. Here's what I used:

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            ElseIf Saved = True Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
            End If
        End If
    End Sub

Its simple:

ElseIf Saved = True Then        
     'This will overwrite it      
     RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
     Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"           
End If

If the file is already saved then path is already there. Just copy the code used in "Saved = false" but only remove the ShowDialog() part.

Hope that helps.

Thanks

Can you help man? Sorry for bothering you anyway, I just want to get this thing work fast.

Does the save works when the Dialog is shown i.e when "Saved = false" ?

See if this works:

Dim savedPath as String

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
                savedPath = SaveFileDialog1.FileName
            ElseIf Saved = True Then
                RichTextBox1.SaveFile(savedPath, RichTextBoxStreamType.TextTextOleObjs)
            End If
        End If
    End Sub

Does the save works when the Dialog is shown i.e when "Saved = false" ?

See if this works:

Dim savedPath as String

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
                savedPath = SaveFileDialog1.FileName
            ElseIf Saved = True Then
                RichTextBox1.SaveFile(savedPath, RichTextBoxStreamType.TextTextOleObjs)
            End If
        End If
    End Sub

Yes, I have tested the dialog and it works when "Save = false". Unfortunately, using a string as you suggested won't do the work. Any other suggestions? BTW, I really appreciate that you still reply.

Its confusing. Can you post or pm me all of your code?

Thanks

Its confusing. Can you post or pm me all of your code?

Thanks

Public Class Form1
    Dim Saved As Boolean = False

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If RichTextBox1.Text Is "" Then
            Application.Exit()
        ElseIf Saved = False Then
            If MessageBox.Show("Are you sure you would like to exit without saving?", "NoteEditor Pro", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Saved = True
                Application.Exit()
                Else
                    e.Cancel = True
                End If
            End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim file As String = Command()
        If Not file = "" Then
            file = Replace(file, Chr(34), "")
            RichTextBox1.LoadFile(file, RichTextBoxStreamType.PlainText)
            Me.Text = file + " - NoteEditor Pro"
        Else
            RichTextBox1.Text = ""
            Me.Text = "Untitled.txt - NoteEditor Pro"
        End If
    End Sub

    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        RichTextBox1.DeselectAll()
    End Sub

    Private Sub FindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Click
        FindText.ShowDialog()
    End Sub

    Private Sub UndoToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem1.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem1.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem1.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem1.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem1.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SelectAllToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem1.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub DeselectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeselectAllToolStripMenuItem.Click
        RichTextBox1.DeselectAll()
    End Sub

    Private Sub TextDocumentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextDocumentToolStripMenuItem.Click
        If RichTextBox1.Text Is "" Then
            Me.Text = "Untitled.txt - NoteEditor Pro"
            RichTextBox1.Clear()
        ElseIf RichTextBox1.Text IsNot Nothing Then
            If MessageBox.Show("Are you sure you would like to create a new project without saving?", "NoteEditor Pro", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Me.Text = "Untitled.txt - NoteEditor Pro"
                RichTextBox1.Clear()
            Else
                If Saved = False Then
                    If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                        RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                        Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                    ElseIf Saved = True Then

                    End If
                End If
            End If
        End If
        Saved = False
    End Sub

    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        Try
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.Font = FontDialog1.Font
            End If
        Catch objException As System.ArgumentException
            MessageBox.Show("This application currently only supports TrueType fonts.", "NoteEditor Pro", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
    End Sub

    Private Sub TextColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextColorToolStripMenuItem.Click
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            RichTextBox1.ForeColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub HourDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HourDateToolStripMenuItem.Click
        RichTextBox1.Text = RichTextBox1.Text + DateTime.Now
    End Sub

    Private Sub ReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceToolStripMenuItem.Click
        ReplaceText.ShowDialog()
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        SettingsForm.ShowDialog()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub NoteEditorProProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoteEditorProProjectToolStripMenuItem.Click
        If RichTextBox1.Text Is "" Then
            Me.Text = "Untitled.ntp - NoteEditor Pro"
            RichTextBox1.Clear()
        ElseIf RichTextBox1.Text IsNot Nothing Then
            If MessageBox.Show("Are you sure you would like to create a new project without saving?", "NoteEditor Pro", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Me.Text = "Untitled.ntp - NoteEditor Pro"
                RichTextBox1.Clear()
            Else
                If Saved = False Then
                    If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                        RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                        Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                        Saved = True
                        RichTextBox1.Clear()
                    ElseIf Saved = True Then

                    End If
                End If
            End If
        End If
        Saved = False
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            Me.Text = "" & OpenFileDialog1.FileName & " - NoteEditor Pro"
        End If
    End Sub

    Private Sub [B]SaveAsToolStripMenuItem[/B]_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
            Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
            Saved = True
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            ElseIf Saved = True Then

            End If
        End If
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBarToolStripMenuItem.Click
        If StatusBarToolStripMenuItem.Checked = False Then
            StatusStrip1.Visible = False
        ElseIf StatusBarToolStripMenuItem.Checked = True Then
            StatusStrip1.Visible = True
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim lineNumber As Integer
        If Not RichTextBox1.Text Is Nothing Then
            lineNumber = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart())
            ToolStripStatusLabel1.Text = "Line: " & lineNumber
        End If
    End Sub
End Class

You'll notice that I want to get the "Save" function working under some other conditions as well but, what I need you to do is to focus on the "SaveAsToolStripMenuItem" item. I made it bold so you can find it more easily.

Oh!.. The problem was with bad placement of "End If".

Try this code:

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            End If
        ElseIf Saved = True Then
        
        RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs) 

        End If
    End Sub

Thanks

Oh!.. The problem was with bad places of "End If".

Try this code:

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            EndIf
        ElseIf Saved = True Then
        
        RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs) 

        End If
    End Sub

Thanks

YES, YES, YES!!! IT WORKED!!! Woohooo!! Thank you man, you're AWESOME!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.