0
votes

I'm doing a HW assignment (as always, don't indirectly make me a cheater, help me learn!) and I'm having a LOT of trouble with the listbox.

I currently have it able to move/remove single items (fruits selecteditem => myFruits)

However I need to be able to move ALL the items at once, (removing them is easy).

Google hasn't quite helped, it seems a lot of answers are way too complex for beginner C# or they're deprecated horribly.

So the question is: how to I transfer the contents of one listbox into another? (Listbox1 name: fruits. Listbox2 name: myFruits)

3
What kind of list box? Windows Forms? Web Forms? SilverLight?John Saunders
Sorry, it was a Windows Forms. The first answer helped me immediately though, so sorry about the lateness.Sterling Archer

3 Answers

2
votes

You could use the following strategy:

  1. Copy selected fruits (fruits.SelectedItems) in a new list
  2. Loop through this list, and for each fruit:
    • Remove it from the fruits ListBox. (fruits.Items.Remove)
    • Add it to the myFruits ListBox. (fruits.Items.Add)
1
votes

To move all of the items, try going in reverse order:

for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
  listBox2.Items.Add(listBox1.Items[i]);
  listBox1.Items.RemoveAt(i);
}

A more practical method for moving all of the "selected" items would be to first loop through all of the selected items and add those to the second list box:

for (int i = 0; i < listBox1.SelectedItems.Count; i++) {
  listBox2.Items.Add(listBox1.SelectedItems[i]);
}

And then to remove those items from listBox1, it's easier to go in reverse order:

for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--) {
  listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
}

The reason for the reverse order is simplicity. If you remove items in forward order, the the "next" index would be off by one, and you would have to manually keep track of your indexes. Hence, going in reverse order avoids that issue.

1
votes

I have some properties that i used a while ago in a similar project... Hope this helps as it doesn't give you the complete answer but gives you a starting point based on your homework with two lists of fruits.

    public List<string> NotAddedAssets
    {
        get
        {
            List<string> notAddedAssets = new List<string>();

            for (int i = 0; i < lbNotAddingAssets.Items.Count; i++)
                notAddedAssets.Add(lbNotAddingAssets.Items[i].ToString());

            notAddedAssets.Sort();

            return notAddedAssets;
        }
        set
        {
            lbNotAddingAssets.AppendDataBoundItems = true;
            lbNotAddingAssets.Items.Clear();
            value.Sort();
            lbNotAddingAssets.DataSource = value;
            lbNotAddingAssets.DataBind();
        }
    }

    public List<string> AddedAssets
    {
        get
        {
            List<string> addedAssets = new List<string>();

            for (int i = 0; i < lbAddingAssets.Items.Count; i++)
                addedAssets.Add(lbAddingAssets.Items[i].ToString());

            addedAssets.Sort();

            return addedAssets;
        }
        set
        {
            lbAddingAssets.AppendDataBoundItems = true;
            lbAddingAssets.Items.Clear();
            value.Sort();
            lbAddingAssets.DataSource = value;
            lbAddingAssets.DataBind();
        }
    }