Save .txt with different name

apelpisia

New member
Joined
Mar 9, 2009
Messages
1
Programming Experience
Beginner
Hi, I'm totally new to the programming area. I'm using VB 2008 and i'm trying to save three textboxes in a text format. I have tried some code but my problem is that i don't want to save always at the same name (test.txt). How can I do it.

Here is the code that i'm using (and probably has lot of mistakes :'( )

Public Class Form1
Const mForm1Name As String = "c:\test.txt"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form1 As System.IO.StreamWriter
Form1 = My.Computer.FileSystem. _
OpenTextFileWriter(mForm1Name, True)
With SaveFileDialog1
.InitialDirectory = "c:\"
.Filter = "Text files | *.txt"
.ShowDialog()
End With
Form1.WriteLine(TextBox1.Text)
Form1.WriteLine(TextBox2.Text)
Form1.WriteLine(TextBox3.Text)
Form1.Close()

End Sub

End Class

-----------------
i understand that the problem is on the top where i've placed Const mForm1Name As String = "c:\test.txt", but when i'm taking it out gives back a mistake. What can i do?
Also my form doesn't close when i save
 
First up, why would you call an object that writes to a text file "Form1"? It's not a form for a start, plus it just creates confusion when the current form is also called "Form1". Use descriptive names for your variables. Use a name that describes what the object is, what it does or both. I'm sure you can come up with a name that would tell YOU what the object is for. If that was someone else's code and you read "Form1" would it tell you what the object was?

As for the question, the idea of showing the SaveFileDialog is to get the path of the file to save, so obviously you can't open that file until the user has selected the path. As such, you can't call OpenTextFileWriter until you've called ShowDialog.

You should also be testing the result of ShowDialog because the user might click the Cancel button. If they do click the OK button then you can get the selected path from the FileName property, which you can then use to open the file.
 
You Almost Have it, you just have to add to the filter.



VB.NET:
Public Class Form1
Const mForm1Name As String = "c:\test.txt"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form1 As System.IO.StreamWriter
Form1 = My.Computer.FileSystem. _
OpenTextFileWriter(mForm1Name, True)
With SaveFileDialog1
.InitialDirectory = "c:\"
.Filter = "Text Files (*.txt)|*.txt|Rich Text Format(*.rtf)|*.rtf|XML Files (*.xml)|*.xml|Batch Files (*.bat)|*.bat|Visual Basic Script (*.vbs) |*.vbs |Information File (*.ini)|*.ini|All Files|*.*"

.ShowDialog()
End With
Form1.WriteLine(TextBox1.Text)
Form1.WriteLine(TextBox2.Text)
Form1.WriteLine(TextBox3.Text)
Form1.Close()

End Sub

End Class
 
Or

VB.NET:
 Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|Rich Text Format(*.rtf)|*.rtf|XML Files (*.xml)|*.xml|Batch Files (*.bat)|*.bat|Visual Basic Script (*.vbs) |*.vbs |Information File (*.ini)|*.ini|All Files|*.*"
        SaveFileDialog1.DefaultExt = ".rtf"
        SaveFileDialog1.ShowDialog()
        If SaveFileDialog1.FileName <> "" Then
            FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
            PrintLine(1, RichTextBox1.Text)
            FileClose(1)
        End If
    End Sub

Of course you can play with this, and change it around, but this is a good basis.
 
Or

VB.NET:
 Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|Rich Text Format(*.rtf)|*.rtf|XML Files (*.xml)|*.xml|Batch Files (*.bat)|*.bat|Visual Basic Script (*.vbs) |*.vbs |Information File (*.ini)|*.ini|All Files|*.*"
        SaveFileDialog1.DefaultExt = ".rtf"
        SaveFileDialog1.ShowDialog()
        If SaveFileDialog1.FileName <> "" Then
            FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
            PrintLine(1, RichTextBox1.Text)
            FileClose(1)
        End If
    End Sub

Of course you can play with this, and change it around, but this is a good basis.
Yick. Under no circumstances should anyone be suggesting that old VB6-style I/O to anyone. Stick with System.IO. Also, as I suggested, you really should do this:
VB.NET:
If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
so that you only act if the user presses OK.
 
Back
Top