[RESOLVED] Got Saving. Now how do i open from a text file
I would like to ask. I have saving to a text file down now with multiple lines and it works.
How do i open this text file into a label.
I know about open file dialog which is the same as savefiledialog.
So how do i open the text file and display the text in a label with multiple lines using the one click of a button and a open file window thingy.
Does anyone have code that could me with this.
Re: Got Saving. Now how do i open from a text file
Use IO.StreamReader:
vb Code:
Dim sr As New IO.StreamReader(locationoffile)
Me.Label1.Text = sr.ReadToEnd()
sr.Close()
Re: Got Saving. Now how do i open from a text file
Remember how I showed you the SaveFileDialog? Well you can use the OpenFileDialog in the exact same way with a couple minor tweaks:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using ofd As New OpenFileDialog With {.AddExtension = True, _
.DefaultExt = "txt", _
.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox1.Lines = IO.File.ReadAllLines(ofd.FileName)
End If
End Using
End Sub
Re: Got Saving. Now how do i open from a text file
Forum account is there anything i should Dim or public at all.
An i think that opens to a text box how would i change it for a label
Re: Got Saving. Now how do i open from a text file
Quote:
Originally Posted by
dinglu
Forum account is there anything i should Dim or public at all.
In regards to what?
Re: Got Saving. Now how do i open from a text file
If you remember in the other thread, sorry but the other code i got it working and didn't use yours but i had to do this:
Code:
Dim stext1 As String
will i need anything like that
Re: Got Saving. Now how do i open from a text file
Not to read the text, no. If you're trying to get the text into a label (like you mentioned in the other thread) you can do something like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using ofd As New OpenFileDialog With {.AddExtension = True, _
.DefaultExt = "txt", _
.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Using sr As New IO.StreamReader(ofd.FileName)
Me.Label1.Text = String.Empty
Me.Label1.Text = sr.ReadToEnd()
End Using
End If
End Using
End Sub
There are no other variables needed to make that work.
Re: Got Saving. Now how do i open from a text file
Re: Got Saving. Now how do i open from a text file
Quote:
Originally Posted by
minitech
My code doesn't work?
Your code will work, even though you forgot to dispose the StreamReader, but I was showing him the OpenFileDialog because in his other thread he was shown the SaveFileDialog.
Re: Got Saving. Now how do i open from a text file
No not yours minitech, just have not looked at yours yet because it looks very simple and most of the time extremely simple code can lead to bugs. nothing personal just have used forum accounts code once before and it was useful.
So forum if i just place that code straight in all i need is the label and button set up and it will create the OFD by itself.
Re: Got Saving. Now how do i open from a text file
He already knew how to use the OpenFileDialog (post #1).
"forgot" is also the wrong word. You don't need to Dispose anything.
dinglu:
Have you actually tried either piece of code?
Personally, I prefer 3 lines of simple code to 11 complicated ones.
Re: Got Saving. Now how do i open from a text file
No i said i Knew about the OFD, i dont exactly know how to use it 100%, just know what it does
Re: Got Saving. Now how do i open from a text file
Sorry, I misunderstood.
:blush:
Re: Got Saving. Now how do i open from a text file
yes i have used forum accounts code an i am about to test the code
Re: Got Saving. Now how do i open from a text file
Well Now the code works looks like i will resolve this thread Now.
Thanks Forum Account your code for OFD was extremely USEFUL