In this exercise, you learn how to create a list box that allows the user to select
Question:
In this exercise, you learn how to create a list box that allows the user to select more than one item at a time. Open the Multi Solution.sln file contained in the VB2017\Chap05\Multi Solution folder. The interface contains a list box named lstNames. The list box’s Sorted and SelectionMode properties are set to True and One, respectively.
a. Open the Code Editor window. The frmMain_Load procedure adds five names to the lstNames control. Code the btnSingle_Click procedure so that it displays, in the lblResult control, the item selected in the list box. For example, if the user clicks Debbie in the list box and then clicks the Single selection button, the name Debbie should appear in the lblResult control.
b. Save the solution and then start the application. Click Debbie in the list box, then click Ahmad, and then click Bill. Notice that when the list box’s SelectionMode property is set to One, you can select only one item at a time in the list.
c. Click the Single selection button. The name Bill appears in the lblResult control. Click the Exit button.
d. Change the list box’s SelectionMode property to MultiSimple. Save the solutionand then start the application. Click Debbie in the list box, then click Ahmad, then click Bill, and then click Ahmad. Notice that when the list box’s SelectionMode property is set to MultiSimple, you can select more than one item at a time in the list. Also notice that you click to both select and deselect an item. (You also can use Ctrl1click and Shift1click, as well as press the spacebar, to select and deselect
items when the list box’s SelectionMode property is set to MultiSimple.) Click the Exit button.
e. Change the list box’s SelectionMode property to MultiExtended. Save the solution and then start the application. Click Debbie in the list, and then click Jim. Notice that in this case, clicking Jim deselects Debbie. When a list box’s SelectionMode property is set to MultiExtended, you use Ctrl1click to select multiple items in the list. You also use Ctrl1click to deselect items in the list. Click Debbie in the list, then Ctrl1click Ahmad, and then Ctrl1click Debbie.
f. Next, click Bill in the list, and then Shift1click Jim. This selects all of the names from Bill through Jim. Click the Exit button.
g. As you know, when a list box’s SelectionMode property is set to One, the item selected in the list box is stored in the SelectedItem property, and the item’s index is stored in the SelectedIndex property. However, when a list box’s SelectionMode property is set to either MultiSimple or MultiExtended, the items selected in the list box are stored in the SelectedItems property, and the indices of the items are stored in the SelectedIndices property. Code the btnMulti_Click procedure so that it first clears the contents of the lblResult control. The procedure should then display the selected names (which are stored in the SelectedItems property) on separate lines in the lblResult control.
h. Save the solution and then start the application. Click Ahmad in the list box, and then Shift1click Jim. Click the Multi-selection button. The five names should appear on separate lines in the lblResult control. Click the Exit button.
Step by Step Answer: