hi i want to make a program so that when i put something in a list box it saves the contense to a .txt file then loads it back into the list when you start the program up again
Visual basic 6.0 help me with saving code please
Make a new project with a form named Form1 and a list box named List1. Add this code and run it.
When the form loads it will populate the list box with two items. Clicking on one of the two items in the list box will write that info to the file. (Note this method only writes one line, and deletes everything else in the text file, so if you want multiple entries, you'll have to do something different.)
To read from a file into a list box, use this code with the same form and list box.
Note that this code will add each item, line by line, from the text file into the list box. To really test this, open the file in notepad and add several lines of text to it, save it, then run the program and click the form to see the items added to the list box.
HTH.
| Code: |
|
Private Sub Form_Load() List1.AddItem "this is an item" List1.AddItem "so is this:" End Sub Private Sub List1_Click() Open "c:\file.txt" For Output As #1 Print #1, List1.Text Close End Sub |
When the form loads it will populate the list box with two items. Clicking on one of the two items in the list box will write that info to the file. (Note this method only writes one line, and deletes everything else in the text file, so if you want multiple entries, you'll have to do something different.)
To read from a file into a list box, use this code with the same form and list box.
| Code: |
| Private Sub Form_Click()
Open "c:\file.txt" For Input As #1 Do While Not EOF(1) Line Input #1, rec List1.AddItem rec Loop Close End Sub |
Note that this code will add each item, line by line, from the text file into the list box. To really test this, open the file in notepad and add several lines of text to it, save it, then run the program and click the form to see the items added to the list box.
HTH.
hi thanx but the last code you gave me isnt working the code rec says a type mismatch
Last edited by andy26 on Thu Nov 02, 2006 8:44 pm; edited 1 time in total
Last edited by andy26 on Thu Nov 02, 2006 8:44 pm; edited 1 time in total
Should be under Form_Load and not Form_Click
.
| Code: |
|
Private Sub Form_Load() Open "c:\file.txt" For Input As #1 Do While Not EOF(1) Line Input #1, rec List1.AddItem rec Loop Close End Sub |
| Teddy1 wrote: | ||
Should be under Form_Load and not Form_Click
|
I already used Form_Load() to populate the list box.
| andy26 wrote: |
| hi yhanx but the last code you gave me isnt working the code rec says a type mismatch |
If you have option explicit on, you need to declare any variables.
| Code: |
| Dim rec as String |
o yea thanx i tryed adding a variable but i did it as an integer lol o fogot about string thanx for your help.
You're welcome. If you need more help, don't hesitate to ask.
My bad... i misread
, same with me
. Any help needed don't hesitate to ask 
