Results 1 to 7 of 7

Thread: For or Do statement for Listboxes [Resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244

    For or Do statement for Listboxes [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?

    I cannot figure out the syntax.

    Thanks,

    Jim
    Last edited by jlegan; Apr 21st, 2003 at 08:34 PM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    VB Code:
    1. Dim n As Long, nCount As Long
    2.  
    3. nCount = List1.ListCount - 1
    4. For n = 0 To nCount
    5.     If List1.Selected(n) Then
    6.         MsgBox List1.List(n)
    7.     End If
    8. Next
    That code is for VB6, I don't have VB.Net installed on the computer I'm currently on and don't really know if you would do it in a smarter way with that.

  3. #3
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    VB Code:
    1. Dim i As Integer, j As Integer
    2. Do Until i = List1.SelCount
    3. If List1.Selected(j) = True Then
    4. List2.AddItem List1.List(j)
    5. i = i + 1
    6. End If
    7. j = j + 1
    8. Loop

    edit: almost same post time, it took me a sec to whip up the code. His is shorter go with that.
    Last edited by Mushroom Realm; Apr 21st, 2003 at 07:51 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    Mushroom,

    Where did the second list come in?

  5. #5
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    Oh, I forgot it said to have a messagebox pop up.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    Do you know the syntax for .NET?

  7. #7
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    Post a link to this thread in the .NET forum. They would know, but it might take a little longer.

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