Solved

Duplicating a LISTBOX with Content Existing Already

Posted on 1999-01-09
5
213 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 20 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 67
Spell Check in VB6 13 114
I need help embedding an image as HTML in my vb.net application 3 69
Help me. 3 57
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

831 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