Struggling to test which Listbox items are selected. I want to allow the user to select multiple items in a listbox so that a string can be stored in an array where the list is also stored. It is a rollcall system (a task for my Year 10 students). I just can't get the syntax right for the listbox. The Listbox is set to MultiSelection.
BTW
If Listbox.SelectedItem = true Then
Does not work. It returns an error.
My code below returns the first selected item (in a message box)- but not the others. I'm just going round and round now. there must be an easier way. Thoughts?
Private Sub BtnRollCall1_Click(sender As Object, e As EventArgs) Handles btnRollCall1.Click
Dim ExcursionArray(29, 4) As String
Dim selected As Integer
Dim LoadNames As StreamReader = File.OpenText("ClassList.txt")
For i = 0 To 29
ExcursionArray(i, 0) = (LoadNames.ReadLine())
lbxRollCall.Items.Add(ExcursionArray(i, 0))
Next
For Each SelectedItem As string In lbxRollCall.SelectedItems
selected = lbxRollCall.SelectedIndex
ExcursionArray(selected, 1) = "a"
Next
For x = 0 To 29
If (ExcursionArray(x, 1) = "a") Then
MsgBox(ExcursionArray(x, 0))
End If
Next
End Sub
It appears that what you actually want to do is update a 2D array and set the second 'column' to "a" if the corresponding 'row' is selected in the ListBox
. One way to do that would be like so:
For Each selectedIndex In lbxRollCall.SelectedIndices
ExcursionArray(selectedIndex, 1) = "a"
Next
Another option would be like so:
For i = 0 To ExcursionArray.GetUpperBound(0)
If lbxRollCall.GetSelected(i) Then
ExcursionArray(i, 1) = "a"
End If
Next