JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Multiselect List Boxes

Everyone's very pleased with your new program to sell classical music CDs— except for the Sales department. Why, they want to know, can the user buy only one CD at a time? Well, you explain, the program uses a list box to display the list of CDs, and when the user makes selection, the program orders that CD. They ask: How about using a multiselect list box? So what's that?

This example is called MultiselectListBoxes on the CD-ROM, and it lets you create a multicolumn, multiselect list box at run time. To make the list box a multiselect list box, I'll set its SelectionMode property to MultiExtended; here are the possible values:

  • MultiExtended— Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections.

  • MultiSimple— Multiple items can be selected.

  • None— No items may be selected.

  • One— Only one item can be selected.

To indicate how multiple selections look, I'll also use the list box's SetSelection method, which you can use to set selections; here's what the code looks like:

Dim ListBox1 As ListBox

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1 = New ListBox()
    ListBox1.Size = New System.Drawing.Size(270, 100)
    ListBox1.Location = New System.Drawing.Point(10, 40)
    AddHandler ListBox1.SelectedIndexChanged, AddressOf _
    ListBox1.MultiColumn = True
    ListBox1.SelectionMode = SelectionMode.MultiExtended
    Dim intLoopIndex As Integer
    For intLoopIndex = 1 To 20
        ListBox1.Items.Add("Item " & intLoopIndex.ToString())
    Next intLoopIndex

    ListBox1.SetSelected(1, True)
    ListBox1.SetSelected(3, True)
    ListBox1.SetSelected(5, True)
End Sub

To handle multiple selections, you can use the list box's SelectedItems and SelectedIndices properties. I've added an event handler for the list box's SelectedIndexChanged event to this example, and loop over all items in those collections, reporting which items are selected in text boxes this way:

Private Sub ListBox1_SelectedIndexChanged(ByVal _
    sender As System.Object, ByVal e As System.EventArgs)
    Dim Item As String
    Dim Index As Integer

    TextBox1.Text = "Selected items: "
    For Each Item In ListBox1.SelectedItems
        TextBox1.Text &= Item.ToString() & " "

    TextBox2.Text = "Selected indices: "
    For Each Index In ListBox1.SelectedIndices
        TextBox2.Text &= Index.ToString() & " "
End Sub

You can see the results in Figure 7.8. As you see in that figure, the new list box is created when you click the "Create list box" button, and it supports multiple columns and selections. The new selections also are reported in the two text boxes at bottom; this program is a success.

Figure 7.8: A multiselect, multicolumn list box.
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor