I have a ListBox in my program with SelectionMode set to MultiExtended. The items within the ListBox are added by the user through a TextBox. Currently, the only purpose of selecting an item in the ListBox is to remove those items, which is currently done by right clicking on a selected item.
I want to also allow the user to remove selected items by pressing the Delete key, so long as the ListBox is the focused control. When the focused control becomes any other control I'd like the selected items to clear, which I do by:
Private Sub LstbxTaskIDs_Leave(sender As Object, e As EventArgs) Handles lstbxTaskIDs.Leave
lstbxTaskIDs.ClearSelected()
End Sub
This part works fine, but then if the user clicks back onto the ListBox, but not onto an item (i.e. an empty region of the control) then either the first item, or the previously selected item (depending on if only one item or multiple items were previously selected) are selected automatically. Or, to put this more succintly:
- User selects item(s) in ListBox.
- User clicks elsewhere on the form.
- Selected item(s) are deselected.
- User clicks on empty region of ListBox.
- The first item (or the last selected item) are automatically selected.
The only change I'd like is for no items to become automatically selected upon re-entering the ListBox. (Note: an item will only be automatically selected if an item was previously selected. If you click on an empty region before ever selecting an item, no item becomes automatically selected.)
This is what I tried, but it doesn't seem to change anything:
Private Sub LstbxTaskIDs_Enter(sender As Object, e As EventArgs) Handles lstbxTaskIDs.Enter
lstbxTaskIDs.ClearSelected()
End Sub
I have also tried replacing lstbxTaskIDs.ClearSelected()
with lstbxTaskIDs.SelectedItems.Clear()
and lstbxTaskIDs.SelectedItem = -1
BeginInvoke(new Action(Sub() lstbxTaskIDs.ClearSelected()))
β Jimi