Results 1 to 3 of 3

Thread: For or Do statement for Listboxes in VB.NET [Resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244

    For or Do statement for Listboxes in VB.NET [Resolved]

    I have a listbox (currently project is in VB.NET but could do in VB6. The listbox contains a list of items. What would the syntax be for a For or Do statement that would perform an action for each item that is checked\highlighted?
    IE. 6 names in a listbox, 3 are highlighted. What would the statement look like if I wanted a msgbox to pop-up for each of their names. Below is the syntax for VB6, I cannot figure out the syntax for VB.NET.

    VB Code:
    1. Dim n As Long, nCount As Long
    2. nCount = List1.ListCount - 1
    3. For n = 0 To nCount
    4.     If List1.Selected(n) Then
    5.         MsgBox List1.List(n)
    6.     End If
    7. Next
    Last edited by jlegan; Apr 21st, 2003 at 08:33 PM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try a for each loop and use the selecteditems collection of the listbox.
    VB Code:
    1. Dim obj As Object
    2.         For Each obj In ListBox1.SelectedItems
    3.             MsgBox(obj.ToString)
    4.         Next

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    Thanks, I changed in to List1.CheckedItems because I changed it to a CheckedListBox.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width