14 Replies - 3832 Views - Last Post: 21 June 2010 - 01:18 PM Rate Topic: -----

#1 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Save Menu Option Problem

Posted 20 June 2010 - 04:46 AM

Hi All,

I am writing a text editor for a college assignment and my problem is when I click save from the file menu, it is bringing up the save file dialog even though the file has already been saved using the save as menu option. I just want the save menu option to update the current file.

I spent last night re-writing my code with the help of someone from another forum, but now I get the blue line under the word "SaveFileDialog" in the code statement saying "Reference to a non shared member requires an object reference".

Please see my code below.

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        Dim strFileName As String
        Dim strFilePath As String = ".\"
        SaveFileDialog.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveFileDialog.FilterIndex = 2
        SaveFileDialog.RestoreDirectory = True


        If strFileName Is Nothing Then
            If SaveFileDialog.ShowDialog = DialogResult.OK Then
                txtBox.SaveFile(Me.SaveFileDialog.FileName)
                strFileName = Me.SaveFileDialog.FileName
            End If
        Else
            txtBox.SaveFile(strFileName)
        End If
    End Sub
End Class



Any help would be much appreciated as I would like to hand my assignment in this coming Friday.

Thanks again

John

Is This A Good Question/Topic? 0
  • +

Replies To: Save Menu Option Problem

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Save Menu Option Problem

Posted 20 June 2010 - 05:31 AM

Have you added a SaveFileDialog to your form and renamed it to SaveFileDialog?
Was This Post Helpful? 1
  • +
  • -

#3 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 20 June 2010 - 05:37 AM

If you havent, then do it. :P
Was This Post Helpful? 1
  • +
  • -

#4 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 20 June 2010 - 05:44 AM

Good call, thanks, however when I press save it brings up the save file dialog, it doesnt update the existing file, has anyone got any idea's

Cheers

John
Was This Post Helpful? 0
  • +
  • -

#5 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 20 June 2010 - 10:48 AM

Well... You need to get the file path of the text document open in your program (maybe store it as a string when you open the file), and then make your program over-write the file when the user clicks save. You don't need to use a save file dialog when doing a normal save, as opposed to a save as.
Was This Post Helpful? 1
  • +
  • -

#6 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 20 June 2010 - 11:28 AM

View PostJack Eagles1, on 20 June 2010 - 09:48 AM, said:

Well... You need to get the file path of the text document open in your program (maybe store it as a string when you open the file), and then make your program over-write the file when the user clicks save. You don't need to use a save file dialog when doing a normal save, as opposed to a save as.


Can you give me an example (code wise).

I know your not supposed to but i am not sure where to start.

Cheers

John
Was This Post Helpful? 0
  • +
  • -

#7 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 20 June 2010 - 11:34 AM

View PostJohn-Ellis, on 20 June 2010 - 10:28 AM, said:

View PostJack Eagles1, on 20 June 2010 - 09:48 AM, said:

Well... You need to get the file path of the text document open in your program (maybe store it as a string when you open the file), and then make your program over-write the file when the user clicks save. You don't need to use a save file dialog when doing a normal save, as opposed to a save as.


Can you give me an example (code wise).

I know your not supposed to but i am not sure where to start.

Cheers

John



Ok so I had a go but it still does the same, have a look and see what you think.

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        Dim CurrentFile As String = "Text Editor - Untitled Document"
        If CurrentFile = "Text Editor - Untitled Document" Then
            SaveAsToolStripMenuItem.PerformClick()
        Else
            My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
        End If
    End Sub



Cheers

John
Was This Post Helpful? 0
  • +
  • -

#8 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 20 June 2010 - 12:13 PM

Are you trying to save, or save as?
Save = Save the document to a file which allready exists (just updating the current allready-present file).
Save As = Save the document to a file which will be newly created by the program (making a new file wherever the user wants to save the document.
Was This Post Helpful? 0
  • +
  • -

#9 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 20 June 2010 - 12:32 PM

View PostJack Eagles1, on 20 June 2010 - 11:13 AM, said:

Are you trying to save, or save as?
Save = Save the document to a file which allready exists (just updating the current allready-present file).
Save As = Save the document to a file which will be newly created by the program (making a new file wherever the user wants to save the document.


Save, my save as sub routine already works, I just cant get the save menu item to work.

Cheers

John
Was This Post Helpful? 0
  • +
  • -

#10 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 20 June 2010 - 02:13 PM

Ok I have had a look at this from another angle, I changed the code in the save as sub routine, I changed the name of the save as menu item to just save, I re-wrote my code so it now looks like this

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
        Dim CurrentFile As String
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
            Else
                My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
            End If
        Catch ex As Exception

        End Try
    End Sub



But I am still getting the same result as before, it saves with the save file dialog box as it should but then instead of just updating the current file it opens the save file dialog box.

I have drawn a proper blank on this if anyone could help me out it would be greatley appreciated.

John
Was This Post Helpful? 0
  • +
  • -

#11 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 20 June 2010 - 02:48 PM

Well the reason which It re-opens the dialog box is that you have put in
SaveAs.ShowDialog
. You need to get the file path of where the file is which you want to over-write, and save it there.

'When you open or save as a file, you will need to set FP to the filepath of the file.
Dim FP as String
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
        Try
                'FP needs to be assigned a value when opening an existing file, or when "Save As-ing" a file. FP needs to be the path of this file (if you see what I mean)
                My.Computer.FileSystem.WriteAllText(FP , txtBox.Text, False)
        Catch ex As Exception
                Messagebox.Show("Error saving: " & ex.ToString
        End Try
End Sub




If you don't understand what I mean here, please post the Open file code on your program, and also the Save As code. If you do that, I can re-write them so that everything will work!

This post has been edited by Jack Eagles1: 20 June 2010 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 21 June 2010 - 10:32 AM

Hi Jack Eagles 1,

Thanks for the offer, I tried again today for about three hours to get it to work and still get the same result as before????? my head is burning lol, please see below the code for

Open Sub Routine

Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Open.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        Open.Title = "Open"
        Try
            If Open.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtBox.Text = My.Computer.FileSystem.ReadAllText(Open.FileName)
                Me.Text = "Text Editor" + Open.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



Save As Sub Routine

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



Save Sub Routine

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        If CurrentFile <> "Text Editor - Untitled Document" Then
            My.Computer.FileSystem.WriteAllText(CurrentFile, txtBox.Text, False)
        Else
            SaveAsToolStripMenuItem.PerformClick()
        End If
    End Sub



It may be worth me mentioning that I placed a global variable for the CurrentFile as apposed to a local one.

Hope you can work some magic my friend.

Thanks very much

John
Was This Post Helpful? 0
  • +
  • -

#13 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 21 June 2010 - 10:52 AM

Okay, here we go:

First declare FP as a global variable (string).

Open File:
Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Open.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        Open.Title = "Open"
        Try
            If Open.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtBox.Text = My.Computer.FileSystem.ReadAllText(Open.FileName)
                Me.Text = "Text Editor" + Open.FileName
                FP = Open.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



For save as:
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
                FP = SaveAs.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



And for Normal save:

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        If FP = "" Then
            SaveAsToolStripMenuItem.PerformClick()
        Else
             My.Computer.FileSystem.WriteAllText(FP, txtBox.Text, False)
        End If
    End Sub



This post has been edited by Jack Eagles1: 21 June 2010 - 10:55 AM

Was This Post Helpful? 1
  • +
  • -

#14 John-Ellis   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: Save Menu Option Problem

Posted 21 June 2010 - 12:44 PM

View PostJack Eagles1, on 21 June 2010 - 09:52 AM, said:

Okay, here we go:

First declare FP as a global variable (string).

Open File:
Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Open.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        Open.Title = "Open"
        Try
            If Open.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtBox.Text = My.Computer.FileSystem.ReadAllText(Open.FileName)
                Me.Text = "Text Editor" + Open.FileName
                FP = Open.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



For save as:
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveAs As New SaveFileDialog()
        Dim FileName As String = SaveAs.FileName
        SaveAs.Filter = "Text Docs(*.txt)|*.txt|All Files(*.*)|*.*"
        SaveAs.Title = "Save"
        Try
            If SaveAs.ShowDialog() = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveAs.FileName, txtBox.Text, False)
                Me.Text = "Text Editor" + SaveAs.FileName
                FP = SaveAs.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub



And for Normal save:

Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click

        If FP = "" Then
            SaveAsToolStripMenuItem.PerformClick()
        Else
             My.Computer.FileSystem.WriteAllText(FP, txtBox.Text, False)
        End If
    End Sub




I really dont know what to say, you truly are the master, thank you very much Jack Eagles I would be stuck here another three weeks if you hadnt helped me out.

I pressed the green button thing for your rep.

A huge thank you mate

John
Was This Post Helpful? 0
  • +
  • -

#15 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Save Menu Option Problem

Posted 21 June 2010 - 01:18 PM

Thats fine :) glad to help, as I really know what it's like to be in your situation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1