Reputation: 21
I'm using a Listbox in my program which is coupled to a dataset. Now I want to select some items in Runtime and get all the values behind those selected items. The values represent the ID's from the Database Entities. I need them for some SQL-queries.
So this is what I've already tried:
For x = 0 To ContracttypeListBox.Items.Count() - 1
If ContracttypeListBox.GetSelected(x) = True Then
MsgBox(ContracttypeListBox.SelectedItems(x)(ContracttypeListBox.ValueMember))
End If
Next
I found the function in the MsgBox using my friend Google. Actually it works quite well but only if I select all items (starting with the first one) in the Listbox. An unselected item among them will cause a System.IndexOutOfRangeException
. The same Problem occurs when I don't select the first item in the list but all the others.
Upvotes: 1
Views: 6226
Reputation:
You can use ListBox.SelectedItems
.
For Each item In ListBox1.SelectedItems
Dim id As Integer = Val(item.ToString)
Next
Upvotes: 0
Reputation: 21
Thanks to Plutonix
This is the solution:
For x = 0 To ContracttypeListBox.SelectedItems.Count() - 1
If ContracttypeBox.SelectedItems.Count() > 0 Then
MsgBox(ContracttypeListBox.SelectedItems(x)(ContracttypeListBox.ValueMember))
End If
Next x
Upvotes: 1