dcsimg
 

Common .NET Controls Crash Course, Part 3: ListBoxes

by Hannes DuPreez
Common .NET Controls Crash Course, Part 3: ListBoxes

Continuing this series on .NET controls, Part 3 will talk about ListBoxes.

What is a ListBox?

A ListBox shows items inside a list. You can select items one by one, or even select multiple items depending on whether the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended.

A few ListBoxes are shown in Figure 1.

Fonts Screen
Figure 1: Fonts Screen

You will see that the Font, Font Style, and Size are ListBoxes.

Using the ListBox

Obviously, you first need to create a Windows Forms application and have at least one form. When the form has loaded, you can have a look in the toolbox for the ListBox. The Toolbox is shown in Figure 2.

Toolbox
Figure 2: Toolbox

Once the ListBox has been added, you may notice the Properties window, as shown in Figure 3. This is where we set the ListBox's properties.

Properties Window
Figure 3: Properties Window

Apart from the ordinary properties, such as Name, Enabled, and so on, here are a few cool properties to look out for.

Cool ListBox Properties

The following properties control how the ListBox works and how it appears:

  • MultiColumn
  • Items
  • SelectionMode
  • SelectedIndex/SelectedIndices

MultiColumn

The MultiColumn ListBox property indicates if the ListBox has more than one column.

Figure 4 shows a ListBox with the MultiColumn Property set to True.

ListBox MultiColumn property
Figure 4: ListBox MultiColumn property

Items

The Items ListBox property lets you to obtain a reference to the list of items in the ListBox, enabling you to add items, remove items, and obtain a count of the items in the collection.

Adding Items

To add items into a ListBox, you make use of the Add method of the Items Property. Here is a quick example:

   private void button1_Click(object sender, EventArgs e)
   {
      listBox2.Items.Add("Item 1");
      listBox2.Items.Add("Item 2");
      listBox2.Items.Add("Item 3");
      listBox2.Items.Add("Item 4");
      listBox2.Items.Add("Item 5");
   }

When the button is clicked, the five items will be added. Figure 5 shows what it looks like.

Items.Add
Figure 5: Items.Add

There is another way to add items. It is a bit trickier because you must create an array, and then add the array to the ListBox. Here's how:

   private void button1_Click(object sender, EventArgs e)
   {

     listBox2.Items.AddRange(new object[]{
         "Array Item 1",
         "Array Item 2",
         "Array Item 3",
         "Array Item 4",
         "Array Item 5"
      });
   }

Figure 6 displays the result of adding an array to a ListBox.

Items.AddRange
Figure 6: Items.AddRange

You also can add items by using the Insert method, and specifying where you'd like the item to be inserted.

Removing Items

You remove items by using the Remove method and specifying the string you'd like to remove, or use the RemoveAt method and specify the item's index you'd like to delete, with 0 being the first item. Here is some code showing removing items both ways:

   private void button2_Click(object sender, EventArgs e)
   {
      listBox1.Items.Remove("Thirteen");

      listBox1.Items.RemoveAt(1);
   }

SelectionMode

You can select items one by one, or even select multiple items depending on if the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended. Here are all the SelectionMode options:

  • MultiExtended: User can use the SHIFT, CTRL, and arrow keys to make selections.
  • MultiSimple: Multiple items can be selected.
  • None: No items can be selected.
  • One: Only one item can be selected.

A short code example follows:

   private void button3_Click(object sender, EventArgs e)
   {
      listBox1.SelectionMode = SelectionMode.MultiExtended;

      listBox1.SetSelected(0, true);
      listBox1.SetSelected(5, true);
      listBox1.SetSelected(9, true);
      listBox1.SetSelected(13, true);
      listBox1.SetSelected(19, true);
  }

I set the SelectionMode property to MultiExtended, and then select five items with the SetSelected method. Figure 7 shows the result.

SelectionMode
Figure 7: SelectionMode

SelectedIndex/SelectedIndices

You use the SelectedIndex to determine which Item was selected, and SelectedIndices to determine which items were selected. Both return the index or indices of the selected items. You also can make use of SelectedItem or SelectedItems to get the selected items or item. Here is a small example:

   private void button4_Click(object sender, EventArgs e)
   {
      MessageBox.Show(listBox1.SelectedItem.ToString());
   }

Conclusion

I hope that this article has helped you understand the intricacies of a ListBox. I would love to do more articles on the basics of controls, so hopefully there will be more.

This article was originally published on Thursday Jan 9th 2020
Home
Mobile Site | Full Site