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

  • Thread starter Thread starter kimiraikkonen
  • Start date Start date
K

kimiraikkonen

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 said:
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'.
 
'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!
 
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
 
kimiraikkonen said:
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 said:
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)'.
 
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...
 
Back
Top