Results 1 to 9 of 9

Thread: [RESOLVED] Listbox Selection Question

  1. #1

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Resolved [RESOLVED] Listbox Selection Question

    I can't seem to figure this one out.

    What I am trying to do is cycle through a large list of items in a ListBox and be able to get the text of whatever item is currently selected.

    I have a listbox with multiselect enabled. I want it to go through a list of names and grab only the ones that are selected and add them to a second listbox.

    Here is what I'm doing.

    VB Code:
    1. Dim i As Integer
    2.    Do until i = lstnames.ListIndex -1
    3.        If lstnames.Selected(i) = True then
    4.               lstdept.AddItem   'this is where I get stuck
    5.        End If
    6.        i = i + 1
    7.    Loop

    I've tried using lstnames.Text, but that only keeps grabbing the first item selected.

    I appreciate the help

    Thanks
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Listbox Selection Question

    Unless I'm missing something, try this:

    VB Code:
    1. Dim i As Integer
    2.    Do until i = lstnames.ListIndex -1
    3.        If lstnames.Selected(i) = True then
    4.               lstdept.AddItem   lstnames.list(i)
    5.        End If
    6.        i = i + 1
    7.    Loop

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Listbox Selection Question

    I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Listbox Selection Question

    Quote Originally Posted by dglienna
    Unless I'm missing something, try this:

    VB Code:
    1. Dim i As Integer
    2.    Do until i = lstnames.ListIndex -1
    3.        If lstnames.Selected(i) = True then
    4.               lstdept.AddItem   lstnames.list(i)
    5.        End If
    6.        i = i + 1
    7.    Loop

    This returns an error saying invalid use of property.

    Thanks tho
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  5. #5

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Listbox Selection Question

    Quote Originally Posted by dee-u
    I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...

    I'm willing to try it if the other way is impossible.

    What would I have to do different to the code I posted above for a checked listbox? I've never used it like that before

    Thanks
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Listbox Selection Question

    Quote Originally Posted by dee-u
    I think even if you have selected many items lstnames.Selected(i) = True will actually be one only. You could try a listbox with a checkbox instead...
    Works for me

    This?

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.         If List1.Selected(i) = True Then
    3.             list2.AddItem List1.List(i)
    4.         End If
    5.     Next


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: Listbox Selection Question

    Quote Originally Posted by manavo11
    Works for me

    This?

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.         If List1.Selected(i) = True Then
    3.             list2.AddItem List1.List(i)
    4.         End If
    5.     Next

    Works like a charm!

    Just what I wanted


    Thank You
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Listbox Selection Question

    Quote Originally Posted by manavo11
    Works for me

    This?

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.         If List1.Selected(i) = True Then
    3.             list2.AddItem List1.List(i)
    4.         End If
    5.     Next
    I stand corrected.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Listbox Selection Question

    Quote Originally Posted by Capp
    Works like a charm!

    Just what I wanted


    Thank You
    You're welcome

    Quote Originally Posted by dee-u
    I stand corrected.


    Has someone helped you? Then you can Rate their helpful post.

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