C# Listbox to ComboBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Effets
    New Member
    • Oct 2011
    • 17

    C# Listbox to ComboBox

    Hello,
    I am working on a project in my C# book
    Tranfer Textbox to Combo,
    Combo to Text,Textbox to list ,but I cant listbox
    to go to Combo.Instead of the word , it puts (collection)
    StatesListBox.I tems.Remove(Sta tesListBox.Sele ctedItem);
    StatesComboBox. Items.Add(State sListBox.Select edItems);
  • arie
    New Member
    • Sep 2011
    • 64

    #2
    ComboBox.Items. Add("some text") method adds only 1 item to your combobox object, and you're trying to add many.

    StatesListBox.S electedItems (what you trying to add) is of a Collection type, because if SelectionMode.M ultiExtended is set in ListBox, you can select more than one item at once. Its ToString() method returns the word "collection ", and that is what's added to your ComboBox.

    If you want to add a collection, a method ComboBox.Items. AddRange(collec tion) should work, or you can change SelectionMode of your ListBox and do:
    Code:
    StatesComboBox.Items.Add(StatesListBox.SelectedItem.ToString())
    Also, why are you removing SelectedItem from ListBox first? Shouldn't you remove it after you add it to the ComboBox?

    Comment

    • Dan Effets
      New Member
      • Oct 2011
      • 17

      #3
      Ok,in the List box you click a Word, then is suppose to removed an sent to the combo box.My latest code attempt :
      Code:
       string item = "";
                  item = Convert.ToString(StatesListBox.SelectedItem);
                  StatesComboBox.Items.Add(item);
                  StatesListBox.Items.Remove(StatesListBox.SelectedItem);
      Now this works however , it pushes the word over and a blank space..How can I get rid of the blank space?
      Word

      Word

      Comment

      • arie
        New Member
        • Sep 2011
        • 64

        #4
        Where are you getting the blank space? In ComboBox or in ListBox?

        If you mean the field that displays selected item in ComboBox, then you can set
        Code:
        StatesComboBox.SelectedIndex = 0;
        And it'll selest the first item on your list (if there are any)

        Or is it in ListBox, after the last item? It's because ListBox won't change size on its own, and if there are less items on it than it can contain, the rest will be blank. It's a normal behaviour. If you really want it gone, you may try changing the size.

        Comment

        • Dan Effets
          New Member
          • Oct 2011
          • 17

          #5
          When push data (word) from ComboBox to listbox OK
          But when I push back to Combobox, the (Word) and extra space show
          up.
          Click Sam in ComBo-> Go ListBox Sam
          Click Sam in ListBox -> Combo Sam and space below it>>??

          Thank you
          Dan

          Comment

          • arie
            New Member
            • Sep 2011
            • 64

            #6
            show the code you use to push from listbox to combobox

            Comment

            • Dan Effets
              New Member
              • Oct 2011
              • 17

              #7
              Code:
                private void StatesComboBox_SelectedIndexChanged(object sender, EventArgs e)
                      {                  
                             StatesListBox.Items.Add(StatesComboBox.SelectedItem);
                             StatesComboBox.Items.Remove(StatesComboBox.SelectedItem);
                          
                          if (StatesComboBox.Items.Count <= 0)
                          {
                              StatesComboBox.SelectedItem = 0;
                              MessageBox.Show("Statebox is now Empty, Please insert a Dollar");
                              Application.Exit();
                          }
                              
                      }
              
              
                      private void StatesListBox_DoubleClick(object sender, EventArgs e)
                      {
                          if (StatesListBox.Items.Count != 0)
                          {
                              StatesComboBox.Items.Add(StatesListBox.SelectedItem);
                              StatesListBox.Items.Remove(StatesListBox.SelectedItem);
                          }
                         
                      }
              This clear the problem...

              Comment

              Working...