Simple thing: How can I check if the user has selected more than one Item in a ListBox? I tried it like this:
If listbox.SelectedItems(1) Then ...
But it returned an out of range exception…
THX for help
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The code you have now is attempting to access the second item in the
SelectedItemscollection, which holds all of the currently selected items in theListBox. This is because the default property ofSelectedItemsisItem, which accepts the zero-based index of an item as a parameter. You are getting an “out of range exception” because there are less than two items currently selected, which means there is no value to return at index = 1.Instead, to check if the user has selected more than one item, you need to use the
Countproperty of theSelectedItemscollection. For example: