Save the contents of a list box to a file

Level:
Level1

A great thing about Visual Basic is that it easily allows you to use standard windows controls. Once you start to get used to using these controls you will quickly find limitations. One such limitation is that they don't have standard save functionality. This is something that many times you will need to implement in order to retain data the user entered between sessions.

The source sample below adds some fake data to a list box. Normally this would be populated in some other way, probably by the end user of your application. The meat of the sample though is in how we open a file up, add all the items from the list and then save the file off.

In order to use this source code, simply create a new project. Add a list box to it and a command button. Double click on the command button and add the code below to its click event handler.

  1. Dim I As Integer
  2.  
  3. List1.AddItem "This is test 1"
  4. List1.AddItem "This is test 2"
  5. List1.AddItem "This is test 3"
  6. List1.AddItem "This is test 4"
  7.  
  8. Open "C:\list.txt" For Append As #1
  9.  
  10. For I = 0 To List1.ListCount - 1
  11.         Print #1, List1.List(I)
  12. Next
  13.  
  14. Close #1

Obviously in a real world application you will want to do things a little more intelligently in regard to the file name, path, and such.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

I have a question sir, how to

I have a question sir, how to save and load a data in a listbox without losing the data if you close the form.
thank you.

i want to know step

sir, how to save data using by system in vb6, i dont know about that. please teach me k??

hai sir...plzz help me...

sir by the way im medel from philippines,a student and studying computer science,,,just want to ask on how to add multiple data to listview and then save it..just like in purchasing an item in an inventory..plzzz help me sir...i need your response...

Hello

How about converting database table files into text file?would it be possible?

help in vb6

sir i make software but when i run in low resolution then my all item is nit set in screen and not look in full screen .i have much item in my form minimum 20 text boxes and very large lable name.so help me how can i adjust my application in all screen .
please send my solution.

Why not write a resize sub

Why not write a resize sub using an algorithm using percentages for your components versus current form size?

Soln.

Pankj Ji
By using SSTab u can solve your problem.

Great salute to u

Dear sir,
I m learning vb from u.
I m really thankful to u ,
bcoz u r my internet vb Guru.

thanks sir,

Learn to spell.

Learn to spell.

A B C D E F G H I J K L M N

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
boys and girls come sing with me . next time wont you sing with me!