stewdaddy
asked on
Assigning selected list box values to an array
I am trying to read the selected items from a list box into an array. The code I have is:
Public Sub Array_lstCells()
Dim a As Integer = lstCells.SelectedItems.Cou nt
Dim arr_lstCells(a)
Dim i As Integer = 0
For Each SelectedItem In lstCells.SelectedItems
arr_lstCells(i) = lstCells.SelectedItems.Ite m(i)
i = i + 1
Next
End Sub
I would also like to display these values in a message box just to make sure it is capturing everything properly.
Public Sub Array_lstCells()
Dim a As Integer = lstCells.SelectedItems.Cou
Dim arr_lstCells(a)
Dim i As Integer = 0
For Each SelectedItem In lstCells.SelectedItems
arr_lstCells(i) = lstCells.SelectedItems.Ite
i = i + 1
Next
End Sub
I would also like to display these values in a message box just to make sure it is capturing everything properly.
ASKER
I never used the StringBuilder object before, so I did a little research and apparently I have to add the line:
Imports System.Text
However, when I added this, "Imports" becomes underlined and it says there is a syntax error. Any advice?
Imports System.Text
However, when I added this, "Imports" becomes underlined and it says there is a syntax error. Any advice?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This worked great. Thanks!
Public Sub Array_lstCells()
Dim a As Integer = lstCells.SelectedItems.Cou
Dim arr_lstCells(a)
Dim i As Integer = 0
Dim sb as New StringBuilder()
For Each sel as Object In lstCells.SelectedItems
arr_lstCells(i) = lstCells.SelectedItems.Ite
i = i + 1
sb.Append(sel.ToString() & vbCrLf)
Next
MessageBox.Show(sb.ToStrin
End Sub