Visual C# (Windows) Guide
Using List Boxes

The list box control is a handy way of storing and displaying items on a form. The following program covers the most basic operations with the control.

The form for this program looks like the screenshot below.

form screenshot

There are four buttons with the names,

  • btnAdd
  • btnRemove
  • btnSelected
  • btnCount

The text box for adding items is called txtAddItem.

The large square in the middle of the form is a listbox called lstMyList. The three items shown in this list were added at design time. Go to the properties window and locate the property called Items. Click the little button and add the three items as shown.

Add Item Procedure

In this procedure, we check that the user has entered an item, if they haven't we tell them off with a message box. If they have, we add the item to the list, empty the text box and place the cursor in the text box ready for the next item.

private void btnAdd_Click(object sender, EventArgs e)
{
   string toadd = txtAddItem.Text;
   if (toadd == "")
   {
      MessageBox.Show("You haven't entered anything", "List Box Messing");
   }
   else
   {
      lstMyList.Items.Add(toadd);
      txtAddItem.Text = "";
      txtAddItem.Focus();
   }
}

Remove Item Procedure

Before we attempt to remove the item, we check that a selection has been made. The SelectedIndex property returns -1 if nothing is selected. If an item is selected, we use its index to remove it.

private void btnRemove_Click(object sender, EventArgs e)
{
   int selected = lstMyList.SelectedIndex;
   if (selected == -1)
   {
      MessageBox.Show("You haven't selected anything","List Box Messing");
   }
   else
   {
      lstMyList.Items.RemoveAt(selected);
   }
}

Selected Procedure

This procedure outputs the selected item to the screen using a message box. Again, we must check that something is selected first. We also need to convert the item to a string in order to display it.

private void btnSelected_Click(object sender, EventArgs e)
{
   int selected = lstMyList.SelectedIndex;
   if (selected == -1)
   {
      MessageBox.Show("You haven't selected anything", "List Box Messing");
   }
   else
   {
      string itemtext = (string)lstMyList.SelectedItem;
      MessageBox.Show("Selected: " + itemtext, "List Box Messing");
   }
}

Count Procedure

This procedure is used to report the number of items in the listbox.

private void btnCount_Click(object sender, EventArgs e)
{
   int mycount = lstMyList.Items.Count;
   MessageBox.Show(mycount + " items in the list.", "List Box Messing");
}

Another Event

We could make it so that when the user double clicks on the listbox, the selected item will be removed. To do this, we need to add a new event. Go to the form that you have designed and select the listbox. Look in the properties window and click on the Events button (lightning bolt). Look for the event called DoubleClick and double click on the word. The following code pops up in the code window.

private void lstMyList_DoubleClick(object sender, EventArgs e)
{

}

Now, copy the code from the remove procedure.

Having A Go

1. Below is the design for a form-based application using the list box control. Design the form as shown and complete the program so that, each time a mark is entered, the list box and text boxes are updated with the correct values.

NumberControlNameText Property
1LabellblMarkEnter Mark
2TextBoxtxtMark 
3ButtonbtnAddAdd Mark
4ListBoxlstMarks 
5LabellblTotalTotal marks
6LabellblNumberNumber Of Marks
7LabellblMeanMean Mark
8TextBoxtxtTotal 
9TextBoxtxtNumber 
10TextBoxtxtMean 
Mean Machine form

2. The combo box control works in a very similar way to the list box. Write a program that allows the user to enter a number of hours worked in a text box, choose from 5 different hourly rates in the combo box and find out how much the total pay is.