Results 1 to 17 of 17

Thread: [RESOLVED]save a file (not save as) SAVE

  1. #1

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Cool [RESOLVED]save a file (not save as) SAVE

    hey one more problem how do I save the file
    The save as works ok but for save if the file has not been saved before the savefiledialog should be called and if any changes were made then the changes should be saved How do I do that?
    Last edited by ethicalhacker; Apr 23rd, 2007 at 08:09 AM.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: save a file (not save as) SAVE

    What file are you talking about??

    Are you saving what's in a RichTextBox or something else?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    Quote Originally Posted by stimbo
    What file are you talking about??

    Are you saving what's in a RichTextBox or something else?
    Yes yes Im making an mdi text editor so the contents of the rtb get saved as a file using rtb.savefile()

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: save a file (not save as) SAVE

    Without trying this out I would imagine you would want to declare a class level variable which indicates if the file has been saved once (i.e. given a path and name - which you would also want to store).

    vb Code:
    1. Private hasBeenSaved As Boolean = False
    2. Private rtbFileName As String

    If the person goes to save the file the first time then check your variable. If False then open the dialog and get the fileName and save it for future reference and set the Boolean to True. Then when they want to save the file again if it's True simply call the SaveFile with the saved FileName variable as the path. Disable any prompts etc...
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: save a file (not save as) SAVE

    To save the contents of a RTB you call its SaveFile method. If you want to save to the same file you opened then you have to know where that file was. That means assigning the file path to a variable when you open it so that you can retrieve that path later. It would make sense to declare a variable in the form that contains the RTB so when you call the Save method of the form it can simply call the RTB's SaveFile method and pass it that path.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: save a file (not save as) SAVE

    Your child form should look something like this:
    vb Code:
    1. Private filePath As String
    2.  
    3. Public Sub Open(ByVal filePath As String)
    4.     Me.RichTextBox1.LoadFile(filePath)
    5.     Me.filePath = filePath
    6. End Sub
    7.  
    8. Public Sub Save()
    9.     Me.RichTextBox1.SaveFile(Me.filePath)
    10. End Sub
    11.  
    12. Public Sub SaveAs(ByVal filePath As String)
    13.     Me.RichTextBox1.SaveFile(filePath)
    14.     Me.filePath = filePath
    15. End Sub
    Then your parent form can just get a reference to the active child form, as we discussed in your other thread, and simply call its Open, Save or SaveAs method. The child form itself does the work of opening and saving the files, as is appropriate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    Theres something wrong with my file save
    When it saves the current file the richtextbox shows the path in it and all other contents go away and when i check the contents of the saved file it contains wierd stuff in it like:
    Code:
    {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\lang1033\f0\fs17 To see The Ultimate Dog Training Guide - A toolkit for training show dogs or any dog. A must for every dog owner,\par
    \par
    please Copy & Paste this link into the Address Bar of your Browser:\par
    http://www.enhancive.net/tr/txt/8/113/\par
    provided by James Pinto\par
    }
    notice the rtf brackets and code at the start and end!?

    Code:
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click
            'Dim SaveFileDialog As New SaveFileDialo
            If currentFile = "" Then
                SaveAsToolStripMenuItem_Click(Me, e)
                Exit Sub
            End If
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            Dim strExt As String
            strExt = System.IO.Path.GetExtension(currentFile)
            strExt = strExt.ToUpper()
            Select Case strExt
                Case ".RTF"
                    child.RichTextBox1.SaveFile(currentFile)
                Case Else
                    Dim txtWriter As System.IO.StreamWriter
                    txtWriter = New System.IO.StreamWriter(currentFile)
                    txtWriter.Write(child.RichTextBox1.Text)
                    txtWriter.Close()
                    txtWriter = Nothing
                    child.RichTextBox1.SelectionStart = 0
                    child.RichTextBox1.SelectionLength = 0
                    child.RichTextBox1.Modified = False
            End Select
            Dim FileName As String = SaveFileDialog1.FileName
            If (child.RichTextBox1.Modified = True And FileName = currentFile) Then
                SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
                If (SaveFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                    child.RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
                                        RichTextBoxStreamType.PlainText)
                End If
            Else
                child.RichTextBox1.SaveFile(currentFile)
                child.RichTextBox1.Text = FileName & currentFile.ToString()
            End If
        End Sub

  8. #8
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save a file (not save as) SAVE

    Quote Originally Posted by ethicalhacker
    Theres something wrong with my file save
    When it saves the current file the richtextbox shows the path in it and all other contents go away and when i check the contents of the saved file it contains wierd stuff in it like:
    Code:
    {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\lang1033\f0\fs17 To see The Ultimate Dog Training Guide - A toolkit for training show dogs or any dog. A must for every dog owner,\par
    \par
    please Copy & Paste this link into the Address Bar of your Browser:\par
    http://www.enhancive.net/tr/txt/8/113/\par
    provided by James Pinto\par
    }
    notice the rtf brackets and code at the start and end!?

    Code:
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click
            'Dim SaveFileDialog As New SaveFileDialo
            If currentFile = "" Then
                SaveAsToolStripMenuItem_Click(Me, e)
                Exit Sub
            End If
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            Dim strExt As String
            strExt = System.IO.Path.GetExtension(currentFile)
            strExt = strExt.ToUpper()
            Select Case strExt
                Case ".RTF"
                    child.RichTextBox1.SaveFile(currentFile)
                Case Else
                    Dim txtWriter As System.IO.StreamWriter
                    txtWriter = New System.IO.StreamWriter(currentFile)
                    txtWriter.Write(child.RichTextBox1.Text)
                    txtWriter.Close()
                    txtWriter = Nothing
                    child.RichTextBox1.SelectionStart = 0
                    child.RichTextBox1.SelectionLength = 0
                    child.RichTextBox1.Modified = False
            End Select
            Dim FileName As String = SaveFileDialog1.FileName
            If (child.RichTextBox1.Modified = True And FileName = currentFile) Then
                SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
                If (SaveFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                    child.RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
                                        RichTextBoxStreamType.PlainText)
                End If
            Else
                child.RichTextBox1.SaveFile(currentFile)
                child.RichTextBox1.Text = FileName & currentFile.ToString()
            End If
        End Sub
    Hi,

    You can also do it this way;

    Code:
     Dim bDirty As Boolean = False
    
    Private Sub  SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  SaveToolStripMenuItem.Click
            If strFileName Is Nothing Then
                If SaveFileDialog.ShowDialog = DialogResult.OK Then
                    rtxtEditor.SaveFile(Me.SaveFileDialog.FileName)
                    strFileName = Me.SaveFileDialog.FileName
                End If
            Else
                rtxtEditor.SaveFile(strFileName)
            End If
            bDirty = False
        End Sub
    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  9. #9

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    Quote Originally Posted by sparrow1
    Hi,

    You can also do it this way;

    Code:
     Dim bDirty As Boolean = False
    
    Private Sub  SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  SaveToolStripMenuItem.Click
            If strFileName Is Nothing Then
                If SaveFileDialog.ShowDialog = DialogResult.OK Then
                    rtxtEditor.SaveFile(Me.SaveFileDialog.FileName)
                    strFileName = Me.SaveFileDialog.FileName
                End If
            Else
                rtxtEditor.SaveFile(strFileName)
            End If
            bDirty = False
        End Sub
    Wkr,

    sparrow1
    That did the same thing
    If the file contents were
    Code:
    mp3fusion.net
    mp3****s.com
    i edited it at runtime adding jam and saved it and now when i open it again it becomes
    Code:
    {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\lang1033\f0\fs17 mp3fusion.net\par
    mp3****s.com\par
    jam\par
    }

  10. #10
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save a file (not save as) SAVE

    Quote Originally Posted by ethicalhacker
    That did the same thing
    If the file contents were
    Code:
    mp3fusion.net
    mp3****s.com
    i edited it at runtime adding jam and saved it and now when i open it again it becomes
    Code:
    {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\lang1033\f0\fs17 mp3fusion.net\par
    mp3****s.com\par
    jam\par
    }
    Hi,

    I've tryed only with the text you gave as example and it works fine for me.
    What is 'adding jam' and how do you open the file again.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  11. #11

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    Quote Originally Posted by sparrow1
    Hi,

    I've tryed only with the text you gave as example and it works fine for me.
    What is 'adding jam' and how do you open the file again.

    Wkr,

    sparrow1
    I meant at runtime I type "Jam" to the open file anyway this is my file open procedure Im using the variable currentFile to store the open file name
    Code:
        Public Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            If child.RichTextBox1.Modified Then
                Dim answer As Integer
                answer = MessageBox.Show("The current document has not been saved, would you like to continue without saving?", "Unsaved Document(", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If answer = Windows.Forms.DialogResult.No Then
                    Exit Sub
                Else
                    OpenFile()
                End If
            Else
                OpenFile()
            End If
        End Sub
    Public Sub OpenFile()
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
            OpenFileDialog1.FilterIndex = 1
            If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) And (OpenFileDialog1.FileName.Length > 0) Then
                If OpenFileDialog1.FileName = "" Then Exit Sub
                Dim strExt As String
                strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
                strExt = strExt.ToUpper()
                Select Case strExt
                    Case ".RTF"
                        Dim FileName As String = OpenFileDialog1.FileName
                        child.RichTextBox1.LoadFile(OpenFileDialog1.FileName, _
                                            RichTextBoxStreamType.PlainText)
                    Case Else
                        Dim txtReader As System.IO.StreamReader
                        txtReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
                        child.RichTextBox1.Text = txtReader.ReadToEnd
                        txtReader.Close()
                        txtReader = Nothing
                        child.RichTextBox1.SelectionStart = 0
                        child.RichTextBox1.SelectionLength = 0
                End Select
                currentFile = OpenFileDialog1.FileName
                child.RichTextBox1.Modified = False
            End If
        End Sub
    I open the file again by going to the destination path and double clicking it
    the file opens in notepad and that weird stuff gets added to the file along with the modifications
    Last edited by ethicalhacker; Apr 23rd, 2007 at 03:34 AM.

  12. #12
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save a file (not save as) SAVE

    Quote Originally Posted by ethicalhacker
    I meant at runtime I type "Jam" to the open file anyway this is my file open procedure Im using the variable currentFile to store the open file name
    Code:
        Public Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            If child.RichTextBox1.Modified Then
                Dim answer As Integer
                answer = MessageBox.Show("The current document has not been saved, would you like to continue without saving?", "Unsaved Document(", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If answer = Windows.Forms.DialogResult.No Then
                    Exit Sub
                Else
                    OpenFile()
                End If
            Else
                OpenFile()
            End If
        End Sub
    Public Sub OpenFile()
            Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
            OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
            OpenFileDialog1.FilterIndex = 1
            If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) And (OpenFileDialog1.FileName.Length > 0) Then
                If OpenFileDialog1.FileName = "" Then Exit Sub
                Dim strExt As String
                strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
                strExt = strExt.ToUpper()
                Select Case strExt
                    Case ".RTF"
                        Dim FileName As String = OpenFileDialog1.FileName
                        child.RichTextBox1.LoadFile(OpenFileDialog1.FileName, _
                                            RichTextBoxStreamType.PlainText)
                    Case Else
                        Dim txtReader As System.IO.StreamReader
                        txtReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
                        child.RichTextBox1.Text = txtReader.ReadToEnd
                        txtReader.Close()
                        txtReader = Nothing
                        child.RichTextBox1.SelectionStart = 0
                        child.RichTextBox1.SelectionLength = 0
                End Select
                currentFile = OpenFileDialog1.FileName
                child.RichTextBox1.Modified = False
            End If
        End Sub
    I open the file again by going to the destination path and double clicking it
    the file opens in notepad and that weird stuff gets added to the file along with the modifications
    Hi,

    Here's my way to open a file:

    Dim bUpdating As Boolean = False

    Code:
    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
            If bDirty = False Then
                bUpdating = True
                If OpenFileDialog.ShowDialog = DialogResult.OK Then
                    rtxtEditor.LoadFile(Me.OpenFileDialog.FileName)
                    strFileName = Me.OpenFileDialog.FileName
                    bDirty = False
                End If
            Else
                'Prompt to save first
                bUpdating = True
                Select Case MsgBox("There are unsaved changes.  Do you wish to save changes?", _
                MsgBoxStyle.YesNo)
                    Case MsgBoxResult.Yes
                        mnuFileSave_Click(Me, Nothing)
                    Case MsgBoxResult.No
                        'Do nothing, allow open
                End Select
                If OpenFileDialog.ShowDialog = DialogResult.OK Then
                    rtxtEditor.LoadFile(Me.OpenFileDialog.FileName)
                    strFileName = Me.OpenFileDialog.FileName
                    bDirty = False
                End If
                bUpdating = False
            End If
        End Sub
    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  13. #13

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    The code
    Code:
     child.RichTextBox1.LoadFile(Me.OpenFileDialog1.FileName)
    gets highlighted in yellow and I get the error 'File Format is not valid'

  14. #14
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save a file (not save as) SAVE

    Quote Originally Posted by ethicalhacker
    The code
    Code:
     child.RichTextBox1.LoadFile(Me.OpenFileDialog1.FileName)
    gets highlighted in yellow and I get the error 'File Format is not valid'
    Hi,

    I think you know now what the problem is with your text changes. The format isn't valid.

    Why are you using a Child.RichTextBox?

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  15. #15

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    Quote Originally Posted by sparrow1
    Hi,

    I think you know now what the problem is with your text changes. The format isn't valid.

    Why are you using a Child.RichTextBox?

    Wkr,

    sparrow1
    because im creating an mdi notepad application so to call the current active mdi child and load the file into its richtextbox is use
    Code:
     Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
    Child.RichTextBox1.loadfile()
    This was working perfectly before but the save file wasnt so now when i added your code for the saving ang open file Im now getting this error
    So now what do I have to do to make it work?
    I dint understand the error what file format isnt valid this was working perfectly before

  16. #16
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: save a file (not save as) SAVE

    Hi,

    You can perhaps Save and open the file in you Child form.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  17. #17

    Thread Starter
    Banned
    Join Date
    Dec 2006
    Location
    India
    Posts
    89

    Re: save a file (not save as) SAVE

    hey there was a silly mistake
    there wasnt a
    Code:
     _
    RichTextBoxStreamType.PlainText)
    in the code u gave me
    Anyway so the file open is working but
    ok even the save file is working but its just like before its still saving that crap rtf{} crappy shuff in the file
    Last edited by ethicalhacker; Apr 23rd, 2007 at 05:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width