Visual Basic 6.0 Guide
The Naughty List Program

1. Launch Visual Basic 6.0 using the start menu, and select Standard Exe from the New Project dialog box. Change the name of the form to frmNaughty and the caption to The Naughty List. Save your project in a new folder.

2. Create a form that looks something like the one below. The white boxes are listboxes (hover over the tools until you see the tooltip popup). The first one should be called lstAll. This box will contain the names of all of the pupils in the class. Call the second listbox lstNaughty. This list will contain the names of all of the naughty pupils. The four command buttons should be called cmdAdd, cmdRemove, cmdWhich and cmdListNaughty. You use the label control to add the titles for the listboxes.

form design

3. Now we need to add some names to the left hand listbox. Click once on the listbox and scroll down the list of properties in the properties window until you find List. There is a dropdown box into which you can type a list of names. Add about 10 names to the list.

What's going on? - We're creating a class list for a teacher who keeps forgetting who has been naughty. When someone is naughty we need to be able to select their name on the list and add it to the list on the right. If they behave themselves well for a while, we want to remove them from the naughty list. The other two buttons will be used for some extra bits of programming fun.

4. Double click on the Add button. You see two lines of code already there. This is the event handling code for dealing with what happens when we press the Add button. Remember we add our code in between these two lines.

We need to work out which item has been selected in the first list and then add it to the second. With listboxes you first work out the number of the selected item and then use that to find the name. We need 2 variables. We need an integer variable to store the position of the item in the list and we need a string to store the name of the person. So we declare these variables with the following lines of code.

Dim intPosition as integer
Dim strName as string

Next we work out which item has been selected. We do this with the following line of code.

intPosition = lstAll.listIndex

If nothing is selected we don't want the program to do anything so we check for this with the following line

If intPosition = -1 Then Exit Sub

Now we find out the name

strName = lstAll.List(intPosition)

And add it to the listbox ion the right hand side.

lstNaughty.AddItem strName

The full listing is (remember to check the names that you used for the listboxes).

Private Sub cmdAdd_Click()
Dim intPosition As Integer
Dim strName As String
intPosition = lstAll.ListIndex
If intPosition = -1 Then Exit Sub
strName = lstAll.List(intPosition)
lstNaughty.AddItem strName
End Sub

5. Test Your Program and get ready to remove an item. Double click the Remove button. You first need to check which of the items is selected. You can do this by modifying the code you used in Step 4 to work out which item was selected. Remember you need to make sure that the program doesn't try to remove a name if nothing has been selected (that can lead to your program crashing).

In order to remove an item you need a line of code like this where intPosition is a variable equal to the position of the selected name.

lstNaughty.RemoveItem intPosition

6. Test Your Program and get ready for some fun. Double click on the Which? Button. Here we want to get the program to pop a message box telling us the name of the currently selected pupil. First, look at what you have done before and work out the code to find the name of the pupil selected in the list on the left. You will need to add the following line where stName is a variable equal to the name of the pupil selected.

MsgBox strName

7. Test Your Program and get ready for too much fun. The List Naughty button should message box each of the names in the naughty list one after another. If there are no items in the list you bail out.

You will need to be able to work out how many names there are in the naughty list. You can do this by creating a variable (an integer) and making it equal to lstNaughty.listCount.

The first item in the list has the number 0. To look at each item in the list you need to use a for loop. This is like a counter. We will start it at 0 and end it at one less than the number of items in the list. The first line of the loop is as follows where intListCount is equal to the number of names on the naughty list.

For intCounter = 0 To intListCount - 1

Each time the loop runs, the value of the variable intCounter is increased by 1. Write some code that ends up giving you a variable equal to the name in the list at the position intCounter. Message box this name like you did before.

Finally, finish the loop with the following line of code.


Test your code and iron out any problems.

8. It's time you tried a few things out for yourself. Can you work out how to make it so that you can double click on a name to add or remove it from the lists? Try double clicking on the list box in and selecting the double click event at the top of the screen. (Timesaving tip - You can reuse the code you wrote for the add event by just typing the name of the subroutine cmdAdd_Click).