Move selected items from one listbox to another in C# winform

前端 未结 7 1695
闹比i
闹比i 2020-12-06 02:41

I\'m trying to move selected items in list box1 to list box2, and vice versa. I have two buttons, >> and <<. When I select items in lis

7条回答
  •  余生分开走
    2020-12-06 03:16

    There will be conflicts for every deleted row, so go with the below code:

    >>

         for (int intCount = ListBox1.SelectedItems.Count - 1; intCount >= 0; intCount--) 
         {
            ListBox2.Items.Add(ListBox1.SelectedItems[intCount]);
            ListBox1.Items.Remove(ListBox1.SelectedItems[intCount]);
         } 
    

    <<

         for (int intCount = ListBox2.SelectedItems.Count - 1; intCount >= 0; intCount--)
         {
            ListBox1.Items.Add(ListBox2.SelectedItems[intCount]);
            ListBox2.Items.Remove(ListBox2.SelectedItems[intCount]);
         }     
    

    If the above one doesn't work then try this:

    while (ListBox1.SelectedItems.Count > 0) 
    { 
        ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);  
        ListBox1.SelectedItems[0].Remove(); 
    }
    

    For more type of answers you can go with this link

提交回复
热议问题