Results 1 to 8 of 8

Thread: Is there a way to copy items from one listbox to the other?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Is there a way to copy items from one listbox to the other?

    Hi there everyone! I am working on a small prog where I need to be able to just copy the items from one listbox to another. Would anyone know of a simple way to copy whatever is in list1 into list2?

    Thank you so much!!

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Is there a way to copy items from one listbox to the other?

    I think this question has come up before! You might find an example floating around this forum or the VB6.0 and Earlier code bank.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Is there a way to copy items from one listbox to the other?

    One very simple way is to loop through list1 and add items to list2
    Code:
    dim i as integer
    list2.clear
    For i=0 to List1.listcount-1
         list2.additem list1.list(i)
    Next

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Is there a way to copy items from one listbox to the other?

    Or, if you just want to move/copy some of the items from one listbox to another listbox (but not all of the items)
    Code:
    Private Sub Command1_Click()
    Dim i As Long
    For i = List2.ListCount - 1 To 0 Step -1 ' counts list entries
        If List2.Selected(i) = True Then ' if selected then
            List1.AddItem List2.List(i) ' add to other list
            List2.RemoveItem i ' optional - use only if you wish to remove the item from the source listbox
        End If
    Next
    End Sub

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Is there a way to copy items from one listbox to the other?

    uhmmmm?

    Code:
    Listbox2.List=Listbox1.List
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Is there a way to copy items from one listbox to the other?

    Quote Originally Posted by Zvoni View Post
    uhmmmm?

    Code:
    Listbox2.List=Listbox1.List
    uhmmmm?

    That does not work in VB6, results in argument not optional error

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Is there a way to copy items from one listbox to the other?

    Damn! it works in VBA.... *grml*
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Is there a way to copy items from one listbox to the other?

    OK, since i confused everyone (again) by stepping into the VB/VBA-trap and its differences, here is a solution i found to copy the complete contents of one listbox to another listbox:

    http://www.devx.com/vb2themax/Tip/19097
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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