How to Select Items in a ListBox in Visual Basic

A list box control is the way to go in Visual Basic if you're going to display many items for the user to select. The problem is for the user when they need to select 90 percent of the items, which can be time consuming. The best method is to pre-select all the items for the user and let them unselect the ones they don't need. In VB, the "Items" property allows you obtain a reference to all the items in the control that you can use to select them.

Instructions

    • 1

      Launch Microsoft Visual Basic Express, click "New Project ..." on the left pane of your screen, and double-click "Windows Forms Application" to start a new project.

    • 2

      Double-click "ListBox" on the Toolbox pane to add a new list box control. Add a button by double-clicking "Button" on the toolbox.

    • 3

      Right-click the list box control and click "Properties." Scroll down until you reach SelectionMode and set it to "MultiSimple."

    • 4

      Double-click the form to create a form load event and add the following code to add five items to the list box:

      Me.ListBox1.Items.Add("January")

          Me.ListBox1.Items.Add("February")

      Me.ListBox1.Items.Add("March")

      Me.ListBox1.Items.Add("April")

      Me.ListBox1.Items.Add("May")

    • 5

      Double-click "Button1" to create a click event for the button. Type the following code to select all the items in the list box control:

      Dim itmCntr As Integer

          For itmCntr = 0 To Me.ListBox1.Items.Count - 1

      Me.ListBox1.SetSelected(itmCntr, True)

      Next

    • 6

      Press "F5" to run the program and click "Button1" to select all the items.

Related Searches

References

  • Photo Credit Ryan McVay/Photodisc/Getty Images

You May Also Like

Related Ads

Check It Out

7 Tips for Photographing Your Kids on Halloween