Solved

Assigning selected list box values to an array

Posted on 2008-10-01
4
214 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now