You are viewing our Forum Archives. To view or take place in current topics click here.
How do I save a txt file?[VB]
Posted:

How do I save a txt file?[VB]Posted:

GamerTechz
  • TTG Addict
Status: Offline
Joined: Feb 11, 201212Year Member
Posts: 2,185
Reputation Power: 88
Status: Offline
Joined: Feb 11, 201212Year Member
Posts: 2,185
Reputation Power: 88
How do I save and load a .txt file? I would like to know, +rep for you! If you post..
#2. Posted:
PoisonOak
  • Resident Elite
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
code to open:

private sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
Dim ofDialog As New OpenFileDialog
ofDialog.Filter = "Txt Files|*.txt"
ofDialog.Title = "Open Text File"
ofDialog.ShowDialog()
Try
Dim strm As New System.IO.StreamReader(ofDialog.FileName)
TextBox1.Text = strm.ReadToEnd
strm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Code to save:
private sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim svDialog As New SaveFileDialog
svDialog.Filter = "Txt Files|*.txt"
svDialog.Title = "Save Text File"
svDialog.ShowDialog()
Try
Dim savestrm As New System.IO.StreamWriter(svDialog.FileName)
savestrm.Write(TextBox1.Text)
savestrm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
#3. Posted:
Vectorizing
  • Prospect
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
PoisonOak wrote code to open:

private sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
Dim ofDialog As New OpenFileDialog
ofDialog.Filter = "Txt Files|*.txt"
ofDialog.Title = "Open Text File"
ofDialog.ShowDialog()
Try
Dim strm As New System.IO.StreamReader(ofDialog.FileName)
TextBox1.Text = strm.ReadToEnd
strm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Code to save:
private sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim svDialog As New SaveFileDialog
svDialog.Filter = "Txt Files|*.txt"
svDialog.Title = "Save Text File"
svDialog.ShowDialog()
Try
Dim savestrm As New System.IO.StreamWriter(svDialog.FileName)
savestrm.Write(TextBox1.Text)
savestrm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


Incase you didnt know you need 2 buttons on the form.

One named - btnOpen
and another - btnSave
and a textbox named - textbox1

just trying to help!
#4. Posted:
SomeWhiteGuy
  • Challenger
Status: Offline
Joined: Oct 22, 201014Year Member
Posts: 109
Reputation Power: 5
Status: Offline
Joined: Oct 22, 201014Year Member
Posts: 109
Reputation Power: 5
PoisonOak wrote code to open:

private sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
Dim ofDialog As New OpenFileDialog
ofDialog.Filter = "Txt Files|*.txt"
ofDialog.Title = "Open Text File"
ofDialog.ShowDialog()
Try
Dim strm As New System.IO.StreamReader(ofDialog.FileName)
TextBox1.Text = strm.ReadToEnd
strm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Code to save:
private sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim svDialog As New SaveFileDialog
svDialog.Filter = "Txt Files|*.txt"
svDialog.Title = "Save Text File"
svDialog.ShowDialog()
Try
Dim savestrm As New System.IO.StreamWriter(svDialog.FileName)
savestrm.Write(TextBox1.Text)
savestrm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


I like the ingredients of your copy and pasta. /facepalm

Where ever you copied that from over complicated such an easy task.

This is how you would do it, OP:

Open

Dim ofd As New OpenFileDialog()
With ofd
.Filter = "Text Files(*.txt)|*.txt"
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolders.Desktop)
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = IO.File.ReadAllText(.FileName)
End If
End With

Save

Dim sfd As New SaveFileDialog
With sfd
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolders.MyDocuments)
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
IO.File.WriteAllText(.FileName, TextBox.Text)
End If
End With

I might have some arguments mixed up/some spelling mistakes but hopefully you'll be smart enough to do that.
#5. Posted:
PoisonOak
  • Resident Elite
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
s0mewhiteguy7 wrote
PoisonOak wrote code to open:

private sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
Dim ofDialog As New OpenFileDialog
ofDialog.Filter = "Txt Files|*.txt"
ofDialog.Title = "Open Text File"
ofDialog.ShowDialog()
Try
Dim strm As New System.IO.StreamReader(ofDialog.FileName)
TextBox1.Text = strm.ReadToEnd
strm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Code to save:
private sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim svDialog As New SaveFileDialog
svDialog.Filter = "Txt Files|*.txt"
svDialog.Title = "Save Text File"
svDialog.ShowDialog()
Try
Dim savestrm As New System.IO.StreamWriter(svDialog.FileName)
savestrm.Write(TextBox1.Text)
savestrm.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


I like the ingredients of your copy and pasta. /facepalm

Where ever you copied that from over complicated such an easy task.

This is how you would do it, OP:

Open

Dim ofd As New OpenFileDialog()
With ofd
.Filter = "Text Files(*.txt)|*.txt"
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolders.Desktop)
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = IO.File.ReadAllText(.FileName)
End If
End With

Save

Dim sfd As New SaveFileDialog
With sfd
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolders.MyDocuments)
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
IO.File.WriteAllText(.FileName, TextBox.Text)
End If
End With

I might have some arguments mixed up/some spelling mistakes but hopefully you'll be smart enough to do that.


it wasnt even copy and pasted kid.. it's called i dont waste my time trying to make everything i post nice and neat. Sorry for trying to help.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.