December 5, 1995
This article explains how to save and later retrieve the contents of the Microsoft® Visual Basic® version 4.0 List Box control to and from a file on disk.
The Microsoft® Visual Basic® version 4.0 List Box control lets you display a list of items, such as employee names, to your user. In your Visual Basic application, you may allow your user to add new items to the control. Before you quit your application, however, you need to save the entries in the List Box control to a file on disk. Then, when your application is run at a later time, you can retrieve the items to the List Box control from the data file's contents.
You can save the contents of a List Box control to a sequential file on disk by first saving each entry in the control to an array. To do this, you would run the following code fragment:
For i = 0 To List1.ListCount - 1
A(i) = List1.List(i)
Next I
The total number of items stored in the List Box control is retrieved from the ListCount property. Next, the array A(i) is created, where the i variable holds a count of the current item being processed. As each item is retrieved from the List Box control, it is saved in the array.
At this point, you use the Visual Basic Write # statement to save each item in the array to a disk file. Similarly, when you reload the items from the disk file to the List Box control, you use the Input # statement.
This program shows how to save the contents of a List Box control to a file on disk. In addition, you are shown how to retrieve the same items from the data file to the List Box control.
Dim A(50) As String
Dim ItemCount As Integer
Private Sub Form_Load()
For x = 1 To 20
List1.AddItem "This is item #" & x
Next x
End Sub
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
A(i) = List1.List(i)
Next i
ItemCount = List1.ListCount
Open "c:\temp.txt" For Output As #1
For i = 0 To ItemCount - 1
Write #1, A(i)
Next i
Close #1
MsgBox "Data has been saved"
End Sub
Private Sub Command2_Click()
List1.Clear
Open "c:\temp.txt" For Input As #1
Do Until EOF(1)
Input #1, b$
List1.AddItem b$
Loop
Close #1
End Sub
Run the example program by pressing F5. The program displays twenty items in the List Box control. Click the first Command Button control. The contents of the List Box control are saved to a file on disk. Click the second Command Button control. The contents of the file on disk are retrieved into the List Box control.