How to store/read listbox items in a text file line by line (with line break) ?

Discussion in 'Microsoft VB .NET' started by kimiraikkonen, Oct 30, 2007.

  1. Hi,
    I want to save all the item content of a listbox line by line into a
    simple text file then recall them when my project is opened.

    For example listbox1 contains:

    That - item1
    Group -item2
    Is -item3
    Really -item4
    Good -item5

    And the text file must be 1:1 same then i have to load/syncronize
    textfile to listbox when the project is opened.

    Should i use streamreader/writer or is there another flexible class
    that helps?

    How can i do that?

    Thanks.
     
    kimiraikkonen, Oct 30, 2007
    #1
    1. Advertisements

  2. "kimiraikkonen" <> schrieb:
    > I want to save all the item content of a listbox line by line into a
    > simple text file then recall them when my project is opened.
    >
    > For example listbox1 contains:
    >
    > That - item1
    > Group -item2
    > Is -item3
    > Really -item4
    > Good -item5
    >
    > And the text file must be 1:1 same then i have to load/syncronize
    > textfile to listbox when the project is opened.
    >
    > Should i use streamreader/writer or is there another flexible class
    > that helps?


    'My.Computer.FileSystem.ReadAllText', 'My.Computer.FileSystem.WriteAllText',
    'Split', 'Join'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
     
    Herfried K. Wagner [MVP], Oct 30, 2007
    #2
    1. Advertisements

  3. On Oct 30, 11:50 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
    > wrote:
    > "kimiraikkonen" <> schrieb:
    >
    >
    >
    >
    >
    > > I want to save all the item content of a listbox line by line into a
    > > simple text file then recall them when my project is opened.

    >
    > > For example listbox1 contains:

    >
    > > That - item1
    > > Group -item2
    > > Is -item3
    > > Really -item4
    > > Good -item5

    >
    > > And the text file must be 1:1 same then i have to load/syncronize
    > > textfile to listbox when the project is opened.

    >
    > > Should i use streamreader/writer or is there another flexible class
    > > that helps?

    >
    > 'My.Computer.FileSystem.ReadAllText', 'My.Computer.FileSystem.WriteAllText',
    > 'Split', 'Join'.
    >
    > --
    > M S Herfried K. Wagner
    > M V P <URL:http://dotnet.mvps.org/>
    > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -
    >
    > - Show quoted text -


    Very thanks but i want to display them in a listbox line by line.

    Dim reader As String
    reader = My.Computer.FileSystem.ReadAllText("c:\test")
    ListBox1.Items.Add(reader)

    When i do that, ALL the text is displayed in the first line as
    combined. But the original "test.txt" file has items line by
    line(seperated with line break) as descibed in my previous message.

    Thanks in advance!
     
    kimiraikkonen, Oct 31, 2007
    #3
  4. On Oct 31, 11:56 am, kimiraikkonen <> wrote:
    > On Oct 30, 11:50 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
    >
    >
    >
    > > wrote:
    > > "kimiraikkonen" <> schrieb:

    >
    > > > I want to save all the item content of a listbox line by line into a
    > > > simple text file then recall them when my project is opened.

    >
    > > > For example listbox1 contains:

    >
    > > > That - item1
    > > > Group -item2
    > > > Is -item3
    > > > Really -item4
    > > > Good -item5

    >
    > > > And the text file must be 1:1 same then i have to load/syncronize
    > > > textfile to listbox when the project is opened.

    >
    > > > Should i use streamreader/writer or is there another flexible class
    > > > that helps?

    >
    > > 'My.Computer.FileSystem.ReadAllText', 'My.Computer.FileSystem.WriteAllText',
    > > 'Split', 'Join'.

    >
    > > --
    > > M S Herfried K. Wagner
    > > M V P <URL:http://dotnet.mvps.org/>
    > > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Very thanks but i want to display them in a listbox line by line.
    >
    > Dim reader As String
    > reader = My.Computer.FileSystem.ReadAllText("c:\test")
    > ListBox1.Items.Add(reader)
    >
    > When i do that, ALL the text is displayed in the first line as
    > combined. But the original "test.txt" file has items line by
    > line(seperated with line break) as descibed in my previous message.
    >
    > Thanks in advance!


    Read the text file line by line (reader.ReadLine() I believe) in a
    loop and add the items that way.

    Thanks,

    Seth Rowe
     
    rowe_newsgroups, Oct 31, 2007
    #4
  5. kimiraikkonen

    Lloyd Sheen Guest

    "kimiraikkonen" <> wrote in message
    news:...
    > On Oct 30, 11:50 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
    > > wrote:
    >> "kimiraikkonen" <> schrieb:
    >>
    >>
    >>
    >>
    >>
    >> > I want to save all the item content of a listbox line by line into a
    >> > simple text file then recall them when my project is opened.

    >>
    >> > For example listbox1 contains:

    >>
    >> > That - item1
    >> > Group -item2
    >> > Is -item3
    >> > Really -item4
    >> > Good -item5

    >>
    >> > And the text file must be 1:1 same then i have to load/syncronize
    >> > textfile to listbox when the project is opened.

    >>
    >> > Should i use streamreader/writer or is there another flexible class
    >> > that helps?

    >>
    >> 'My.Computer.FileSystem.ReadAllText',
    >> 'My.Computer.FileSystem.WriteAllText',
    >> 'Split', 'Join'.
    >>
    >> --
    >> M S Herfried K. Wagner
    >> M V P <URL:http://dotnet.mvps.org/>
    >> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Very thanks but i want to display them in a listbox line by line.
    >
    > Dim reader As String
    > reader = My.Computer.FileSystem.ReadAllText("c:\test")
    > ListBox1.Items.Add(reader)
    >
    > When i do that, ALL the text is displayed in the first line as
    > combined. But the original "test.txt" file has items line by
    > line(seperated with line break) as descibed in my previous message.
    >
    > Thanks in advance!
    >


    Try this (just typed in so it may not be exact)

    Dim reader as string
    reader = My.Computer.FileSystem.ReadAllText("c:\test")

    dim strs() as string
    strs=split(reader,environment.newline) ' get an array of strings

    then either

    for each s as string in strs
    listbox1.items.add(s)
    next

    or

    listbox1.datasource = strs
    listbox1.databind

    Hope this helps
    Lloyd Sheen
     
    Lloyd Sheen, Oct 31, 2007
    #5
  6. "Lloyd Sheen" <> schrieb:
    > Dim reader as string
    > reader = My.Computer.FileSystem.ReadAllText("c:\test")
    >
    > dim strs() as string
    > strs=split(reader,environment.newline) ' get an array of strings
    >
    > then either
    >
    > for each s as string in strs
    > listbox1.items.add(s)
    > next


    .... or 'ListBox1.Items.AddRange(Lines)'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
     
    Herfried K. Wagner [MVP], Oct 31, 2007
    #6
  7. On Oct 31, 6:40 pm, "Lloyd Sheen" <> wrote:
    > "kimiraikkonen" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On Oct 30, 11:50 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
    > > > wrote:
    > >> "kimiraikkonen" <> schrieb:

    >
    > >> > I want to save all the item content of a listbox line by line into a
    > >> > simple text file then recall them when my project is opened.

    >
    > >> > For example listbox1 contains:

    >
    > >> > That - item1
    > >> > Group -item2
    > >> > Is -item3
    > >> > Really -item4
    > >> > Good -item5

    >
    > >> > And the text file must be 1:1 same then i have to load/syncronize
    > >> > textfile to listbox when the project is opened.

    >
    > >> > Should i use streamreader/writer or is there another flexible class
    > >> > that helps?

    >
    > >> 'My.Computer.FileSystem.ReadAllText',
    > >> 'My.Computer.FileSystem.WriteAllText',
    > >> 'Split', 'Join'.

    >
    > >> --
    > >> M S Herfried K. Wagner
    > >> M V P <URL:http://dotnet.mvps.org/>
    > >> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > Very thanks but i want to display them in a listbox line by line.

    >
    > > Dim reader As String
    > > reader = My.Computer.FileSystem.ReadAllText("c:\test")
    > > ListBox1.Items.Add(reader)

    >
    > > When i do that, ALL the text is displayed in the first line as
    > > combined. But the original "test.txt" file has items line by
    > > line(seperated with line break) as descibed in my previous message.

    >
    > > Thanks in advance!

    >
    > Try this (just typed in so it may not be exact)
    >
    > Dim reader as string
    > reader = My.Computer.FileSystem.ReadAllText("c:\test")
    >
    > dim strs() as string
    > strs=split(reader,environment.newline) ' get an array of strings
    >
    > then either
    >
    > for each s as string in strs
    > listbox1.items.add(s)
    > next
    >
    > or
    >
    > listbox1.datasource = strs
    > listbox1.databind
    >
    > Hope this helps
    > Lloyd Sheen


    That Done IT!. Thanks but the last problem how to write into test.txt
    with a "new line". I tried Mr. Wagner's suggestion
    (me.computer.filesystem.writealltext) but i got that error:

    Overload resolution failed because no accessible 'WriteAllText'
    accepts this number.

    I tried:

    Dim writer As String
    writer = My.Computer.FileSystem.WriteAllText("c:\bookmarks.txt")

    How can i write / insert new text into "test.txt" as a NEWline?

    Thanks...
     
    kimiraikkonen, Nov 2, 2007
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JenHu

    How to read text file line by line?

    JenHu, Dec 10, 2004, in forum: Microsoft VB .NET
    Replies:
    3
    Views:
    270
    Mike Labosh
    Dec 10, 2004
  2. androoo

    how to read image from database and store in file

    androoo, Jan 13, 2007, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    146
    androoo
    Jan 13, 2007
  3. ags5406
    Replies:
    3
    Views:
    341
    Satish Itty
    Oct 18, 2007
  4. kimiraikkonen

    Writing listbox items into text file

    kimiraikkonen, Dec 29, 2007, in forum: Microsoft VB .NET
    Replies:
    6
    Views:
    1,500
    kimiraikkonen
    Dec 29, 2007
  5. Tony Bansten
    Replies:
    0
    Views:
    278
    Tony Bansten
    Mar 25, 2009
Loading...

Share This Page