Copy items to another combobox?

  • Thread starter Thread starter james
  • Start date Start date
J

james

Hi,
I am trying to duplicate the contents of a combobox to another.
I tried:

cmbBoxA.Items.AddRange(cmbBoxB.Items);

but it says it can't convert a combobox objectcollection[] to object[].

I tried putting (Object[]) in front of the addrange parameter but get a
similar error.
Any idea? I did something similar with a ListView and it worked fine...

James.
 
This works, although I don't know if it's optimal.

comboBox1.Items.Add("an item");
object[] a= new object[comboBox1.Items.Count];
comboBox1.Items.CopyTo(a,0);
comboBox2.Items.AddRange(a);
 
DeveloperX said:
This works, although I don't know if it's optimal.

comboBox1.Items.Add("an item");
object[] a= new object[comboBox1.Items.Count];
comboBox1.Items.CopyTo(a,0);
comboBox2.Items.AddRange(a);
I'll give it a try - there won't be too many items, so optimal isn't too
much of a worry.

I also lied - copying listview items like that doesn't work! It compiles,
but at runtime, it says I can't have that item in more than one location
unless I clone it?
 
Yeah listviews use ListViewItem objects as opposed to just a bunch of
objects. ListViewItem has a clone method on it for that :)
DeveloperX said:
This works, although I don't know if it's optimal.

comboBox1.Items.Add("an item");
object[] a= new object[comboBox1.Items.Count];
comboBox1.Items.CopyTo(a,0);
comboBox2.Items.AddRange(a);
I'll give it a try - there won't be too many items, so optimal isn't too
much of a worry.

I also lied - copying listview items like that doesn't work! It compiles,
but at runtime, it says I can't have that item in more than one location
unless I clone it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top