Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy List from ComboBox to Another

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
US
There has to be a more efficient way to copy the list from one combo box to another other than iterating through the list and .Add. Any suggestions?


Thanks.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
Not sure why you might want an exact duplicate.

But this should work:

Code:
        Me.ComboBox1.Items.Add("One")
        Me.ComboBox1.Items.Add("Two")
        Me.ComboBox1.Items.Add("Three")

        Dim aItems(Me.ComboBox1.Items.Count - 1) As Object
        Me.ComboBox1.Items.CopyTo(aItems, 0)
        Me.ComboBox2.Items.AddRange(aItems)

As a side note, in a case like this I would build the items into an array from the start and fill each combobox with the AddRange method....

Code:
        Dim cItems() As String = {"One", "Two", "Three"}
        Me.ComboBox1.Items.AddRange(cItems)
        Me.ComboBox2.Items.AddRange(cItems)

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

Just curious...
Code:
 Dim cItems() As String = {"One", "Two", "Three"}
    [red]Me.[/red]ComboBox1.Items.AddRange(cItems)
    [red]Me.[/red]ComboBox2.Items.AddRange(cItems)
What does the [tt]Me.[/tt] really do in this case? It is unnecessary, right? If so, why have it? [tt]Me.[/tt] refers to the Form the control(s) are on.

Is it just a personal preference? Style of programming?


Have fun.

---- Andy
 
More or less a carryover from earlier programming in my case.

In earlier versions of VB and VBA, Me was the easiest method to get Intellisense to work properly. Today's Visual Studio versions don't require it...

While not strictly necessary, I still find it habit.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top