Solved

Assigning selected list box values to an array

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Import a txt file into a DataGridView and TextBox 20 51
Sql server insert 13 36
Help with error in Query 2 31
VB.Net Determine if a mapped network drive exists 2 30
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 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