Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void MoveOne()
        {
            int intNumberOfItems = strAvailableItems.Length;
            if (lsbAvailableList.SelectedItems != null)
            {
                foreach (var item in lsbAvailableList.SelectedItems)
                {

                    //if (lsbAvailableList.SelectedIndex < intNumberOfItems)
                    //{
                    int intLsbAvailableListIndex = lsbAvailableList.SelectedIndex; //get the index
                    lsbSelectedList.Items.Add(item);
                    lsbAvailableList.Items.RemoveAt(intLsbAvailableListIndex);
                    //lsbAvailableList.Items.Remove(lsbAvailableList.SelectedItems[intLsbAvailableListIndex]);
                    string strIndex = intLsbAvailableListIndex.ToString();
                    //int intLsbIndex = lsbAvailableList.Items.Add(item);
                    MessageBox.Show(strIndex);
                    //}
                 }
            }//
         }
        //-----------------------
Posted

Here's an overly simplified way of how to safely remove the currently selected items in a list view, without the use of .Tags.
C#
List<listviewitem> itemsToBeRemoved = new List<listviewitem>();
foreach (ListViewItem item in theListView.SelectedItems) {
    itemsToBeRemoved.Add (item);
}
foreach (ListViewItem lvi in itemsToBeRemoved) {
    theListView.Remove (lvi);
}

/ravi
 
Share this answer
 
v2
You can't remove items from lsbSelectedList.Items while simultaneously querying lsbAvailableList.SelectedItems. I suggest you tag each ListView item (see the .Tag property) and use the tagged values to identify the ones you want to remove.

/ravi
 
Share this answer
 
Comments
Ranjan.D 10-Feb-14 15:28pm    
Just by using for loop instead of using foreach must work I believe.
Ravi Bhavnani 10-Feb-14 15:37pm    
No, it won't. You can't modify a collection you're traversing.

/ravi
Ranjan.D 10-Feb-14 15:52pm    
Yes Ravi. I knew it wasn't with foreach but hopefully there's a way with for loop like the below example. Or use Linq approaches.

var data=new List<string>(){"One","Two","Three"};
for(int i=data.Count - 1; i > -1; i--)
{
if(data[i]=="One")
{
data.RemoveAt(i);
}
}

http://stackoverflow.com/questions/7340757/c-sharp-list-removing-items-while-looping-iterating
You can achieve this with 'foreach:
C#
ListBox.SelectedObjectCollection selListItems = listBox1.SelectedItems;

foreach (var itm in selListItems.OfType<string>().ToList())
{
    listBox1.Items.Remove(itm);
}
By converting the ListBox SelectedItems from ListBoxObjectCollection to List<string>, you can iterate and remove without the collectio modification error.

You could also use
C#
selListItems.OfType<object>().ToList()
in the 'foreach loop.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900