dcsimg

Common .NET Controls Crash Course, Part 3: 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 January 9th, 2020

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date