Question MDI Save As Menu Option

jboy012000

New member
Joined
Aug 5, 2010
Messages
2
Location
UK
Programming Experience
Beginner
Hi All,

I have written an MDI text editor, its almost complete, but I cant get the save as menu option to work correctly.

Please see my code so far.

HTML:
 'This sub routine controls the save as menu option
    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveAs As New SaveFileDialog()
        SaveAs.Filter = "Plain Text Files(*.txt)|*.txt"
        SaveAs.Title = "Save As"
        SaveAs.ShowDialog()
        Try
            cform = New Form2
            i += 1
            cform.Text = SaveAs.FileName + " - Text Editor V.2"
            CF = SaveAs.FileName
            cform.MdiParent = Me
            cform.Show()
            My.Computer.FileSystem.WriteAllText(SaveAs.FileName, (DirectCast(Me.ActiveMdiChild, Form2).RichTextBox1.Text), False)

        Catch ex As Exception

        End Try

    End Sub

So whats happening is that it opens the dialog ok, saves the document to my chosen location correctly, but opens a new document and gives the new document my chosen file name and leaves my saved document with the file name New Document.

But when I close all the open screens and then re-open my saved document its all as it should be.

Can someone take a look at my code and tell me where its going wrong.

Thanks very much

John
 
It's a good idea for your Save and Save As functions to interact.
VB.NET:
Private filePath As String

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, _
                                        ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    Using dialogue As New OpenFileDialog
        With dialogue
            .FileName = Me.filePath
            .Filter = "Text Files (*.txt)|*.txt|All Files|*.*"

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Try
                    Me.TextBox1.Text = IO.File.ReadAllText(dialogue.FileName)
                    Me.filePath = dialogue.FileName
                Catch ex As Exception
                    Debug.WriteLine(ex.ToString())
                    MessageBox.Show(ex.Message, "Open File Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End With
    End Using
End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, _
                                        ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    Me.FileSave()
End Sub

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, _
                                          ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    Me.FileSaveAs()
End Sub

Private Sub FileSave()
    If Me.filePath Is Nothing Then
        Me.FileSaveAs()
    Else
        Try
            IO.File.WriteAllText(Me.filePath, Me.TextBox1.Text)
        Catch ex As Exception
            Debug.WriteLine(ex.ToString())
            MessageBox.Show(ex.Message, "Save File Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End If
End Sub

Private Sub FileSaveAs()
    Using dialogue As New SaveFileDialog
        With dialogue
            .FileName = Me.filePath
            .Filter = "Text Files (*.txt)|*.txt|All Files|*.*"

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Me.filePath = dialogue.FileName
                Me.FileSave()
            End If
        End With
    End Using
End Sub
 
Back
Top