Solved

Duplicating a LISTBOX with Content Existing Already

Posted on 1999-01-09
5
202 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good Points!
0
 

Accepted Solution

by:
edunc earned 20 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updates not working for MS Windows 7 12 122
Using "ScreenUpdating" 6 45
How does CurrentUser work? 10 25
Visual Studio 2005 text editor 10 18
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now