How to add selected Items into a collection

Do you know how to add all selected items from List into a collection?
Please write codes in detail! Thanks!
leeannAsked:
Who is Participating?
 
CArnoldConnect With a Mentor Commented:
LEEANN,
  I'm not sure what type of collection you are trying to place your results into, but here is a short and simple program that you could probably modify to suit your needs.  If you have any problems, let me know!

1) Create a form with a listbox, a textbox, and a command button.

2) For demonstration purposes, we'll use the default names of each control.

3) Change the MultiSelect property of the listbox to either "1-Simple" or "2-Extended"

4) Add the following code:


-------------------------
Option Explicit
Dim x As Integer

Private Sub Command1_Click()

    Text1.Text = ""
    For x = 0 To List1.ListCount - 1
        If List1.Selected(x) Then
            List1.ListIndex = x
            Text1.Text = Text1.Text & vbCr & List1.Text
        End If
    Next x
   
End Sub

Private Sub Form_Load()
    For x = 1 To 10
        List1.AddItem (CStr(x))
    Next x
   
End Sub
------------------------------

   This short program will populate your listbox with numbers 1 thru 10.  Select any of the numbers and click the command button.  Your selected items will appear in the textbox!  Modify this code to place the value(s) into an array, user type, etc.

Good luck!
Charles
0
All Courses

From novice to tech pro — start learning today.