Hello,

I have a form with two list boxes and a button that, when clicked, removes the selected item from the first list box and adds it to the second list box. I can add the selected item to the second list box but i can't find a method that allows me to remove the selected item from the first list box. Any suggestions for implementing this? Below is the snippet of code for the button click event.

public partial class TwoListBoxes : Form
    {
        public TwoListBoxes()
        {
            InitializeComponent();
        }

        private void btnTransferItem_Click(object sender, EventArgs e)
        {
             lstSecond.Items.Add(lstFirst.SelectedItem);
             
        }
    }

listViewBox.SelectedItems[0].Remove()

If this is not what you are looking for please explain a bit more.

listViewBox.SelectedItems[0].Remove()

If this is not what you are looking for please explain a bit more.

I would like to remove any selected item from the list box and not just the first item, which is what your solution does

I would like to remove any selected item from the list box and not just the first item, which is what your solution does

What do you mean with "any selected item"? Do you mean multiple selected items?

The Code Posted will remove any 'SELECTED ITEM' if nothing is selected it will remove the first item.

Now are u looking to remove Multiple items then, I guess you have to put it in a loop.

for (int i = 0; i < listView1.Items.Count; i++ )
{
    if (listView1.Items[i].Selected)
    {
        listView1.Items[i].Remove();
        i--;
    }
}

If I have solved your problem please mark as Solved.

Thanks.

If you want to clear the Whole ListView Then

listView1.Items.Clear();
listBox2.Items.AddRange(listBox1.SelectedItems.Cast<object>().ToArray());
            
listBox1.SelectedItems.OfType<object>().ToList().RemoveAll((p) => { listBox1.Items.Remove(p); return true; });

if solved please mark as solved.

I would like to remove any selected item from the list box and not just the first item, which is what your solution does

What do you mean with "any selected item"? Do you mean multiple selected items?

Maybe i did not explain my problem clearly enough so here goes again. The form has two List Boxes. One on the right and one on the left. The left-side list box contains 5 items. The right-side list box does not contain any item as yet. Beneath the two list boxes is a button labeled Transfer Item. I would like to select any item from the left side list box, and click the button to remove the selected item and place it in the list box on the right. The code snippet i provided transfers the selected item to the right-side list box but does not remove it from the left side list box.
I tried adatapost's solution and it works but it seems too complicated to a C# beginner that i am since i have not yet studied Arrays and objects in C#.
Is there a simpler way to remove and transfer a selected item from one list box to another?

Thanks for understanding.

Maybe i did not explain my problem clearly enough so here goes again. The form has two List Boxes. One on the right and one on the left. The left-side list box contains 5 items. The right-side list box does not contain any item as yet. Beneath the two list boxes is a button labeled Transfer Item. I would like to select any item from the left side list box, and click the button to remove the selected item and place it in the list box on the right. The code snippet i provided transfers the selected item to the right-side list box but does not remove it from the left side list box.
I tried adatapost's solution and it works but it seems too complicated to a C# beginner that i am since i have not yet studied Arrays and objects in C#.
Is there a simpler way to remove and transfer a selected item from one list box to another?

Thanks for understanding.

Sorry m8, first instance I thought we was dealing with ListView.

anyway hope this makes up for my earlier mistake.

listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);

Attached Program if you don't get it.

Sorry m8, first instance I thought we was dealing with ListView.

anyway hope this makes up for my earlier mistake.

listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);

Attached Program if you don't get it.

Thanks . It works now. Actually i just realized that my code is exactly as yours except in reverse order. I was calling the "remove" method before i called the "add" method and was getting a "ArgumentNullException was unhandled" error message (stupid me i guess...lol).

Thanks for making me see the error of my ways.

Cheers.

please mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.