VB6 Project: Saving and loading the contents of a listbox

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:
- Create a listbox on your form and call it lstItems.
- Set the Sorted property of the listbox to True so that items added to the listbox are sorted into alphabetical order.
- Add a textbox to your form and call it txtNewItem
- 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.
- Add a command button to your form and call it cmdNewItem.
- 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()
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.
- The next line adds the contents of the textbox (txtNewItem.Text) to the listbox by using the listbox's AddItem command.
- 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()
Adding the code for 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)
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.
