Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a save file control which will start up a Save file dialouge and right it to text and an open dialouge to rea files. Now when the user opens a previous saved file how can I write a code to a save button where the file would written over the previous file without using a save fle dialog control. Below is my save file dialog for my save a button what would it be for a save button?

VB
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim Save As New SaveFileDialog()
5:
        Dim myStreamWriter As System.IO.StreamWriter
6:
7:
        Save.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
8:
        Save.CheckPathExists = True
9:
        Save.Title = "Save"
10:
        Save.ShowDialog(Me)
11:
12:
        Try
13:
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
14:
            myStreamWriter.WriteLine(TextBox1.Text)
15:
            myStreamWriter.WriteLine(TextBox2.Text)
16:
            myStreamWriter.Flush()
17:
        Catch ex As Exception
18:
            ' Do Nothing
19:
        End Try
20:
    End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 20:56pm    
Last sentence is not clear. Do you mean that you have SaveAs and asking about Save? If so, this is trivial -- the only difference there is no dialog needed as a full file name is stored with a document control. Or are you asking about something else?
--SA
icecode 22-Oct-12 21:39pm    
The code clearly shows a "Save As" function. What I am looking for is way that when a user opens a file taht he has previously saved in his own unique way he can make changes then click save that will make the change in the text file.
Sergey Alexandrovich Kryukov 22-Oct-12 23:51pm    
Sure. Didn't I already answered what to do with "Save"? If it's unclear, I'll put my answer. It also depends on how do you want to present multiple document (or only one per application). This situation is quite usual, pretty much as in the regular text editor.
--SA
Kuthuparakkal 22-Oct-12 22:21pm    
Unclear
icecode 22-Oct-12 22:33pm    
In my menu I have Open File, New File, Save As, and Save. I want to program the save button not the save as.

Simply have a private property filename that you set to Nothing when creating a new file. When a file is opened, you store the filename in there. You also store it when a Save as... has been successfully executed. You now can check that filename property and when save is clicked you only show the dialog if filename is Nothing, otherwise you already know the filename and can overwrite it with a newer version.

Good luck!
 
Share this answer
 
Try:
VB
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    Dim myStream As Stream
    Dim Save As New SaveFileDialog()
    Dim myStreamWriter As System.IO.StreamWriter
    Save.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    'Save.CheckPathExists = True
    Save.Title = "Save"
    
    
    If Save.ShowDialog() = DialogResult.OK Then
        myStream = Save.OpenFile()
        If (myStream IsNot Nothing) Then
            ' Code to write the stream goes here.
            ' !!!set second parameter to StreamWriter as "True", to append!
            Using myStreamWriter As StreamWriter = New StreamWriter(myStream, True) 
                myStreamWriter.WriteLine(TextBox1.Text)
                myStreamWriter.WriteLine(TextBox2.Text)
                myStreamWriter.Close()
                myStream.Close()
            End Using
        End If
    End If
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900