How do I create an array from the selected items in a ListBox?

PaulieORF

Newcomer
Joined
Oct 21, 2004
Messages
24
I'm working on an application which requires me to take the items that a user selects from a multi-select ListBox, and put them into another ListBox on another form. I basically just want to add the selected items in the ListBox to an array, and then call on that array to fill the ListBox on the other form. I've looked all around this site and Google for how to do this, and I all I can really find is how to do it in VB6, I need to know how in .NET. Thanks!
 
I'm kinda new to using arrays, and I have just fiddled around with the CopyTo method, but can't seem to make it work. What is the proper way to declare my array, and what is the proper way to use the ListBox's CopyTo command to fill that array? I appreciate the help, thanks.
 
I would think that in most cases, you could use the .SelectedItems collection in place of an array. If you need the items in the form of an array, however, here:

Visual Basic:
'Try using a more specific type, such as String if the listbox contains strings
        Dim X As Object() 'Declare Variable
        X = New Object(ListBox1.SelectedItems.Count - 1) {} 'Create properly sized array
        ListBox1.SelectedItems.CopyTo(X, 0) 'Copy selected items to the array
 
Back
Top