开发者

VB Check if Multiselect in Listbox

开发者 https://www.devze.com 2023-01-24 22:32 出处:网络
Simple thing: How can I check if the user has selected more than one Item in a Li开发者_JAVA百科stBox? I tried it like this:

Simple thing: How can I check if the user has selected more than one Item in a Li开发者_JAVA百科stBox? I tried it like this:

If listbox.SelectedItems(1) Then ...

But it returned an out of range exception...

THX for help


The code you have now is attempting to access the second item in the SelectedItems collection, which holds all of the currently selected items in the ListBox. This is because the default property of SelectedItems is Item, 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 Count property of the SelectedItems collection. For example:

If listbox.SelectedItems.Count > 1 Then
    ''#your code here
End If


If listbox.SelectedItems.Count() > 1 Then 
0

精彩评论

暂无评论...
验证码 换一张
取 消