By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Listbox to Combo Box

P: 53
Hello All,

I have a tabcontrol control , In the tabpage1 I have listbox with some items, and in the tabpage2 I have a combo box, How to transfer the items(all the items) from the Listbox in tabpage1 to Combo box in tabpage2 without User selecting the items in the listbox.

The below code transfers only one selected item, but I want to transfer all the items....

private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Add(listBox1.SelectedItem);

}

Thank you all....
Feb 11 '09 #1
Share this Question
Share on Google+
8 Replies


P: 53
I have a tabcontrol , In tabpage1 I have a listbox with few Items, In tabpage2
I have ComboBox, I want to copy all the Items in the listbox to combobox without user selecting the Item in the Listbox.

I am trying the following code:


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Add(listBox1.SelectedItem);
}

This works when user selects an item in the listbox . But I want to transfer all the items in the listbox to combobox without selecting the items.

Thank you all....
Feb 11 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
PageTwoCombobox.items = PageOneListbox.items;
Feb 11 '09 #3

P: 53
I want to copy all the items in the listbox to mutlitple combo box, The solution given is not working...its tabpage1 not recognised...........I want to copy to mutlitple combo box
Feb 11 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
The names of the controls are not dependant on the tab pages.
You should be able to copy directly from the item collection of one control to the item collection of another with no reference of which tab page they are on.
So don't try to reference them by pages, just by control names.
Feb 11 '09 #5

P: 53
It says Listbox1 is readonly..

This is how I tried:
listBox1.Items = comboBox1.Items;
Errors:

1. indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only
2.Cannot implicitly convert type 'System.Windows.Forms.ComboBox.ObjectCollection' to 'System.Windows.Forms.ListBox.ObjectCollection'

and I want to copy from a single Listbox1 to mutliptle Combobox...
Feb 11 '09 #6

tlhintoq
Expert 2.5K+
P: 3,525
Please don't think me rude when I ask, "Have you tried a little experimenting?"
Asking for the answers to specific needs may get you answers.
A little effort and experimentation will get you knowledge and understanding.

1. indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only
Since you are trying to read from the ListBox this shouldn't be a problem or even come up. Read only is fine if you are only reading from it to place those items into a ComboBox.

Try this... Loop through the items of the Listbox (your source) and use the .Add method of the ComboBox (your destination)

Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             for (int nIndex = 0; nIndex < listBox1.Items.Count; nIndex++)
  4.             {
  5.                 string bob = listBox1.Items[nIndex] as string;
  6.                 comboBox1.Items.Add(bob);
  7.             }
  8.         }
  9.  
Feb 11 '09 #7

tlhintoq
Expert 2.5K+
P: 3,525
Please don't double post your questions.
http://bytes.com/topic/c-sharp/answe...tbox-combo-box
Feb 11 '09 #8

P: 53
Oh Sorry...I thought the 1st time when I posted the question it didn't go through..I am really sorry for posting it twice...
Feb 12 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.