|
-
May 23rd, 2009, 09:54 PM
#1
Thread Starter
Lively Member
[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.
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 09:56 PM
#2
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()
-
May 23rd, 2009, 09:59 PM
#3
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
-
May 23rd, 2009, 10:03 PM
#4
Thread Starter
Lively Member
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
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 10:05 PM
#5
Re: Got Saving. Now how do i open from a text file
 Originally Posted by dinglu
Forum account is there anything i should Dim or public at all.
In regards to what?
-
May 23rd, 2009, 10:06 PM
#6
Thread Starter
Lively Member
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
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 10:10 PM
#7
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.
Last edited by ForumAccount; May 23rd, 2009 at 10:13 PM.
-
May 23rd, 2009, 10:11 PM
#8
Re: Got Saving. Now how do i open from a text file
-
May 23rd, 2009, 10:12 PM
#9
Re: Got Saving. Now how do i open from a text file
 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.
-
May 23rd, 2009, 10:14 PM
#10
Thread Starter
Lively Member
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.
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 10:15 PM
#11
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.
-
May 23rd, 2009, 10:17 PM
#12
Thread Starter
Lively Member
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
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 10:19 PM
#13
Re: Got Saving. Now how do i open from a text file
Sorry, I misunderstood.
-
May 23rd, 2009, 10:24 PM
#14
Thread Starter
Lively Member
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
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
-
May 23rd, 2009, 10:48 PM
#15
Thread Starter
Lively Member
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
Ha HA Come join me On halo if you dare. Im talking PC not console

Oh and this is out of Date. I use IE 8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|