Click to See Complete Forum and Search --> : Get every item from listbox and write it to a file


AndyK
January 19th, 2000, 11:15 PM
i have "test1" and "test2" in a Listbox, how can I write them into a file so later on I can read from that file and use list1.additem to add strings "test1" and "test2" back into listbox

Thank You

Lothar Haensler
January 20th, 2000, 01:40 AM
to write the listbox contents to a file:

Dim yourfile as string
Dim i as Integer
yourfile = "c:\test.txt"
Dim hfile as Integer
hfile = FreeFile
Open yourfile for Output as #hfile
for i = 0 to lbx.ListCount - 1
print #hfile, lbx.List(i)
next i
Close hfile

AndyK
January 20th, 2000, 02:12 AM
Ok, now how do I read data from that file and reverse the process by putting what I have saved back into listbox

Thank You

Lothar Haensler
January 20th, 2000, 02:16 AM
private Sub Command2_Click()
Dim yourfile as string
Dim i as Integer
yourfile = "c:\test.txt"
Dim hfile as Integer
hfile = FreeFile
Open yourfile for input as #hfile
Dim strline as string
lbx.Clear
Do While Not EOF(hfile)
Line input #hfile, strline
lbx.AddItem strline
Loop
Close hfile

End Sub

AndyK
January 20th, 2000, 02:59 AM
Thanks bud.

Dr_Michael
January 20th, 2000, 05:31 AM
It is faster enough instead of 'for' loop:

for i = 0 to lbx.ListCount - 1
print #hfile, lbx.List(i)
next i



to use 'for each' loop:

dim li as listitem
for Each li In YourListView.ListItems
print #hfile, li
next




Michael Vlastos
Automation Engineer
Company SouthGate Hellas SA
Development Department
Athens, Greece

Lothar Haensler
January 20th, 2000, 06:06 AM
may be you are right, but AndyK asked for a listbox solution. How would a for each loop look on a list box?