Listbox to Combo Box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbandalli
    New Member
    • Feb 2009
    • 53

    Listbox to Combo Box

    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_Select edIndexChanged( object sender, EventArgs e)
    {
    comboBox1.Items .Add(listBox1.S electedItem);

    }

    Thank you all....
  • sbandalli
    New Member
    • Feb 2009
    • 53

    #2
    List box to combo box

    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_Select edIndexChanged( object sender, EventArgs e)
    {
    comboBox1.Items .Add(listBox1.S electedItem);
    }

    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....

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      PageTwoCombobox .items = PageOneListbox. items;

      Comment

      • sbandalli
        New Member
        • Feb 2009
        • 53

        #4
        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

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          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.

          Comment

          • sbandalli
            New Member
            • Feb 2009
            • 53

            #6
            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 .ObjectCollecti on' to 'System.Windows .Forms.ListBox. ObjectCollectio n'

            and I want to copy from a single Listbox1 to mutliptle Combobox...

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              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)

              Code:
                      private void button1_Click(object sender, EventArgs e)
                      {
                          for (int nIndex = 0; nIndex < listBox1.Items.Count; nIndex++)
                          {
                              string bob = listBox1.Items[nIndex] as string;
                              comboBox1.Items.Add(bob);
                          }
                      }

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Please don't double post your questions.

                Comment

                • sbandalli
                  New Member
                  • Feb 2009
                  • 53

                  #9
                  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...

                  Comment

                  Working...