Results 1 to 15 of 15

Thread: [RESOLVED] Got Saving. Now how do i open from a text file

  1. #1

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    Resolved [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

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Got Saving. Now how do i open from a text file

    Use IO.StreamReader:
    vb Code:
    1. Dim sr As New IO.StreamReader(locationoffile)
    2. Me.Label1.Text = sr.ReadToEnd()
    3. sr.Close()

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Using ofd As New OpenFileDialog With {.AddExtension = True, _
    3.                                               .DefaultExt = "txt", _
    4.                                               .Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"}
    5.             If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 Me.TextBox1.Lines = IO.File.ReadAllLines(ofd.FileName)
    7.             End If
    8.         End Using
    9.     End Sub

  4. #4

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Got Saving. Now how do i open from a text file

    Quote Originally Posted by dinglu View Post
    Forum account is there anything i should Dim or public at all.
    In regards to what?

  6. #6

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Using ofd As New OpenFileDialog With {.AddExtension = True, _
    3.                                               .DefaultExt = "txt", _
    4.                                               .Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"}
    5.             If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 Using sr As New IO.StreamReader(ofd.FileName)
    7.                     Me.Label1.Text = String.Empty
    8.                     Me.Label1.Text = sr.ReadToEnd()
    9.                 End Using
    10.             End If
    11.         End Using
    12.     End Sub

    There are no other variables needed to make that work.
    Last edited by ForumAccount; May 23rd, 2009 at 10:13 PM.

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Got Saving. Now how do i open from a text file

    My code doesn't work?

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Got Saving. Now how do i open from a text file

    Quote Originally Posted by minitech View Post
    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.

  10. #10

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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

  11. #11
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  12. #12

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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

  13. #13
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Got Saving. Now how do i open from a text file

    Sorry, I misunderstood.


  14. #14

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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

  15. #15

    Thread Starter
    Lively Member dinglu's Avatar
    Join Date
    May 2008
    Location
    Western Sydney, NSW, Australia
    Posts
    94

    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
  •  



Click Here to Expand Forum to Full Width