How to Remove a Highlight Selection in a Listbox
Removing a highlighted selection in a "ListBox" control can easily be accomplished by using the "Items" property of the "ListBox" control. The "Items" property enables you to obtain a reference to the list of items that are currently stored in a "ListBox." With this reference you can add items, remove items, and obtain a count of the items in the collection. This process can help you set a "ListBox" control to a default state in order for the user to make a different selection.
Instructions
-
-
1
Launch Microsoft Visual Studio, click "New Project" from the left pane of your computer screen, and expand "Visual Basic" below "Installed Templates." Click "Windows," and double-click "Windows Forms Application" from the center of the dialog window to create a new project.
-
2
Double-click "ListBox" to add a new control to your form, then double-click "Button" to add a new button to your form.
-
-
3
Double-click your form to create a form load event and add the following code to add some items to your "ListBox" control:
Me.ListBox1.Items.Add("Monday")
Me.ListBox1.Items.Add("Tuesday")
Me.ListBox1.Items.Add("Wednesday")
Me.ListBox1.Items.Add("Thursday")
Me.ListBox1.Items.Add("Friday")
-
4
Switch back to "Form" design view and double-click "Button1" to create a click event for this button. Add the following code to remove a highlighted selection in the "ListBox" when the button is clicked:
Dim selectedIdx = Me.ListBox1.SelectedIndex
Me.ListBox1.SetSelected(selectedIdx, False)
-
5
Press "F5" to run your program, and click an item on the "ListBox" to highlight it. Click "Button1" to clear the selection.
-
1
References
- Photo Credit Hemera Technologies/Photos.com/Getty Images