Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Duplicating a LISTBOX with Content Existing Already

Posted on 1999-01-09
5
Medium Priority
?
243 Views
Last Modified: 2010-05-03
Is it possible to duplicate a LISTBOX that has content already existing in it so that i can have a second listbox with the EXACT content that was held in the original listbox? So, for example i have:

List1.list(0)="Hello"
List1.list(1)="There"

List2 = list1

Now List2 should have ALL of the same properties, including content as LIST1.

Thanks

0
Comment
Question by:aniston
5 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1455098
You could try something like this:

Private Sub CopyListBox(Source As ListBox, Target As ListBox)
   
    Dim mintCurrentListItem As Integer
    Target.Clear
    For mintCurrentListItem = 0 To Source.ListCount - 1
        With Target
            .AddItem Source.List(mintCurrentListItem)
            .ItemData(.NewIndex) = Source.ItemData(mintCurrentListItem)
        End With
    Next
End Sub

Private Sub cmdCopyContents_Click()
    CopyListBox List1, List2
End Sub
0
 

Author Comment

by:aniston
ID: 1455099
Oh...i am sorry. I should have elaborated more. I already knew i could do the method you just proposed mcix ... Sorry for not mentioning this in my original post. (Edited by Computer101).. The reason i am asking to duplicate a listbox in one single copy is because i notice when you have some 6000 items inside a listbox and you attempt to duplicate these items into another listbox that process takes VERY LONG. I was hoping there was an a better way to copy such a large amount of data into the other at a quicker pace.. Can anyone else provide a solution to this UNIQUE problem.

0
 
LVL 15

Expert Comment

by:ameba
ID: 1455100
It is faster if you cache Source.ListCount - 1 before loop:
    Dim slc As Integer
    slc = Source.ListCount - 1
    For mintCurrentListItem = 0 To slc

and set Visible property of Target listbox
' before Target.Clear
   Target.Visible = False
' after loop
   Target.Visible = True

Warning:
If your Target listbox has Sorted=True, code will execute slow.

And putting 'With Target' outside loop (as mcix did) is very important.
0
 
LVL 4

Expert Comment

by:mcix
ID: 1455101
Good Points!
0
 

Accepted Solution

by:
edunc earned 40 total points
ID: 1455102
woah!  those other explinations were long!  You'll probably like this one a lot better!


  'this is so there is something in list1.  you would probably replace this with something else in your program.
  List1.AddItem "hello"
  List1.AddItem "there"

  'this will copy the contents of list1 to list2
  For i = 0 To List1.ListCount - 1
    List2.List(i) = List1.List(i)
  Next i

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question