Solved

Assigning selected list box values to an array

Posted on 2008-10-01
4
220 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:stewdaddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22618534
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
0
 

Author Comment

by:stewdaddy
ID: 22618635
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?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 22618718
yes, you need System.Text, but it should work. StringBuilder allows you faster string concatenating, but if you want to try with pure strings:

Public Sub Array_lstCells()
        Dim a As Integer = lstCells.SelectedItems.Count
        Dim arr_lstCells(a)
        Dim i As Integer = 0
        Dim sb as String = ""

        For Each sel as Object In lstCells.SelectedItems
            arr_lstCells(i) = lstCells.SelectedItems.Item(i)
            i = i + 1
            sb = sb & sel.ToString() & vbCrLf
        Next
        MessageBox.Show(sb)
    End Sub
0
 

Author Closing Comment

by:stewdaddy
ID: 31502112
This worked great.  Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question