Link to home
Start Free TrialLog in
Avatar of stewdaddy
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.Count
        Dim arr_lstCells(a)
        Dim i As Integer = 0

        For Each SelectedItem In lstCells.SelectedItems
            arr_lstCells(i) = lstCells.SelectedItems.Item(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.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

try with the following:

Public Sub Array_lstCells()
        Dim a As Integer = lstCells.SelectedItems.Count
        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.Item(i)
            i = i + 1
            sb.Append(sel.ToString() & vbCrLf)
        Next
        MessageBox.Show(sb.ToString())
    End Sub
Avatar of stewdaddy
stewdaddy

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?
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked great.  Thanks!