VB6 Project: Saving and loading the contents of a listbox

Image showing what controls to place on the form

The image above shows you what controls you will need for this project. Starting with a blank form add the following controls and set their properties as shown:

  1. Create a listbox on your form and call it lstItems.
  2. Set the Sorted property of the listbox to True so that items added to the listbox are sorted into alphabetical order.
  3. Add a textbox to your form and call it txtNewItem
  4. Set the MaxLength property of the textbox to 30. This will stop the user from entering names that are too long to display nicely in the listbox.
  5. Add a command button to your form and call it cmdNewItem.
  6. Change the Caption property of the command button to Add to list.

You are now ready to start adding the code to your project.

Adding the code for the command button

Double-click on the command button. This will create the empty sub-routine that will be triggered when the user clicks the button. Add the code shown below (I've also shown the start and end of the sub-routine - you don't need to add those, you should already have them):


Private Sub cmdAddNewItem_Click()

    ' This sub is called when the user clicks the Add button
   
    ' Check if the txtNewItem is empty and exit the Sub if it is
    If txtNewItem.Text = "" Then Exit Sub
   
    ' Add the text in txtNewItem to my list
    lstItems.AddItem (txtNewItem.Text)
   
    ' Empty the text box, ready for the next name
    txtNewItem.Text = ""
End Sub

The first line that you need to add is to check if the user has entered anything in the textbox (you don't want to add an empty line to the listbox). If the textbox is empty then exit the sub-routine.

  1. The next line adds the contents of the textbox (txtNewItem.Text) to the listbox by using the listbox's AddItem command.
  2. Finally, the contents of the textbox is cleared by setting the Text property of the textbox to an empty string (two double-quotes with nothing in-between).

Adding the code for the Form_Load event

Going back to your form, double-click on an empty area of your form. This will create the empty sub-routine that will be triggered when the form loads - when the program starts running. Edit your sub-routine so that it looks like this:


Private Sub Form_Load()

    ' This sub is triggered when the program first runs and
    ' the form is loaded
   
    ' Create a string variable called strFileLine
    Dim strFileLine As String
   
    ' Clear the list before loading the data
    lstItems.Clear
   
    ' Open the text file for input
    Open App.Path & "\data.txt" For Input As #1
   
    ' Keep looping as long as the end of the file hasn't been reached
    While Not EOF(1)
 
        ' Get a line of input from the file and store it in strFileLine
        Line Input #1, strFileLine
 
        ' Add the string (strFileLine) to the list
        lstItems.AddItem (strFileLine)
    Wend
   
    ' Close the file
    Close #1
   
End Sub 

Adding the code for the Form_Unload event

Select the Form Unload event

Still in the code editor, make sure that in the top-left box has Form selected and, using the right-hand drop-down box, select Unload (see the image above). This should create the empty sub-routine that is triggered when the form unloads. The unload event happens as your program is being closed. Edit the Form_Unload sub-routine so that it looks like this:


Private Sub Form_Unload(Cancel As Integer)

    ' This sub-routine is triggered when the program is closing
   
    ' Create an integer variable called index
    Dim index As Integer
   
    ' Open the text file for Output as file number 1
    Open App.Path & "\data.txt" For Output As #1
 
    ' Loop through all the list items (the first item in the list is 0)
    ' lstItems.ListCount is the number of items in lstItems
    For index = 0 To lstItems.ListCount – 1
 
           ' Write item(index) from the list to file number 1
             Print #1, lstItems.List(index)
    Next
   
    ' Close file number 1
    Close #1
 
End Sub

As the form is closing, it saves the data in the listbox.

Before you run the program, create an empty text file called "data.txt" in the same folder as where you save your project.