18

I have a listbox being populated from a SQLDATA pull, and it pulls down some columns that i dont want like OBJECT_dfj, OBJECT_daskd. The key is all of these being with OBJECT_, is there a way to remove these from the listbox? I cannot change my SQL statement.

i tried this:

 foreach (string item in listBox1.Items)
 {
     string removelistitem = "OBJECT";
     if(item.Contains(removelistitem))
     {
         listBox1.Items.Remove(item);
     }
 }

but it gave me the error:

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

7
  • How is it being populated, via code or databinding? Is this WPF? WinForms? ASP.NET? Jan 19, 2010 at 19:41
  • Winform, and its being populated from a SQLCOMMAND
    – user222427
    Jan 19, 2010 at 19:43
  • This doesn't make any sense. The WinForms ListBox can only hold one column of text unless you're doing custom drawing. Are you concatenating the contents of all the fields, or what?
    – Ryan Lundy
    Jan 19, 2010 at 19:49
  • What yes this is more then possible... i've done it tons of times. Hwo do i simply remove what i need to remove from the listbox.
    – user222427
    Jan 19, 2010 at 19:52
  • When you say it pulls down "columns" you don't want, what you appear to mean is that you're ending up with items in your ListBox that you don't want. You're not displaying them as columns, but as a list.
    – Ryan Lundy
    Jan 19, 2010 at 21:13

16 Answers 16

23

You can't use an enumerator, you have to loop using an index, starting at the last item:

for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
    string removelistitem = "OBJECT";
    if (listBox1.Items[n].ToString().Contains(removelistitem))
    {
        listBox1.Items.RemoveAt(n);
    }
}
5

You can't modify the references in an enumerator whilst you enumerate over it; you must keep track of the ones to remove then remove them.

This is an example of the work around:

List<string> listbox = new List<string>();
List<object> toRemove = new List<object>();

foreach (string item in listbox)
{
    string removelistitem = "OBJECT";
    if (item.Contains(removelistitem))
    {
        toRemove.Add(item);
    }
}

foreach (string item in toRemove)
{
    listbox.Remove(item);
}

But if you're using c#3.5, you could say something like this.

listbox.Items = listbox.Items.Select(n => !n.Contains("OBJECT"));
2
  • 1
    it won't work as a cut and paste job, it's a clue more than an answer as i don't have an IDE to hand. Jan 19, 2010 at 20:26
  • 1
    beware the premature optimizer Oct 20, 2015 at 9:51
3

You want to iterate backwards through using a counter instead of foreach. If you iterate forwards you have to adjust the counter as you delete items.

for(int i=listBox1.Items.Count - 1; i > -1; i--) {
{
    if(listBox1.Items[i].Contains("OBJECT"))
    {
        listBox1.Items.RemoveAt(i);
    }
}
7
  • Error 1 'object' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    – user222427
    Jan 19, 2010 at 20:07
  • I believe if(listBox1.Items[i].Contains(removelistitem)) should read if(((string)listBox1.Items[i]).Contains(removelistitem)).
    – Jeremy S
    Jan 19, 2010 at 20:14
  • InvalidArgument=Value of '117' is not valid for 'index'. Parameter name: index
    – user222427
    Jan 19, 2010 at 20:16
  • 1
    Iteration needs to start at listBox1.Items.Count - 1
    – egrunin
    Mar 1, 2012 at 14:35
  • I like "interate", whether deliberate or not; it makes sense. Oct 7, 2014 at 17:55
1

You can do it in 1 line, by using Linq

listBox1.Cast<ListItem>().Where(p=>p.Text.Contains("OBJECT")).ToList().ForEach(listBox1.Items.Remove);
0

You can't modify a collection while you're iterating over it with foreach. You might try using a regular for() statement.

You may need to iterate backwards from the end of the collection to make sure you cover every item in the collection and don't accidentally overrun the end of the collection after removing an item (since the length would change). I can't remember if .NET accounts for that possibility or not.

0

The problem here is that you're changing your enumerator as you remove items from the list. This isn't valid with a 'foreach' loop. But just about any other type of loop will be OK.

So you could try something like this:

for(int i=0; i < listBox1.Items.Count; )
{
    string removelistitem = "OBJECT";
    if(listBox1.Items[i].Contains(removelistitem))
         listBox1.Items.Remove(item);
    else
        ++i;
}
1
  • Error 1 'object' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
    – user222427
    Jan 19, 2010 at 20:08
0

You could try this method:

List<string> temp = new List<string>();

    foreach (string item in listBox1.Items)
    {
        string removelistitem = "OBJECT";
        if(item.Contains(removelistitem))
        {
            temp.Items.Add(item);
         }
     }

    foreach(string item in temp)
    {
       listBox1.Items.Remove(item);
    }

This should be correct as it simply copies the contents to a temporary list which is then used to delete it from the ListBox.

Everyone else please feel free to mention corrections as i'm not 100% sure it's completely correct, i used it a long time ago.

0

The error you are getting means that

foreach (string item in listBox1.Items)

should be replaced with

for(int i = 0; i < listBox1.Items.Count; i++) {
    string item = (string)listBox1.Items[i];

In other words, don't use a foreach.

EDIT: Added cast to string in code above

EDIT2: Since you are using RemoveAt(), remember that your index for the next iteration (variable i in the example above) should not increment (since you just deleted it).

4
  • its giving me : Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
    – user222427
    Jan 19, 2010 at 20:12
  • try string item = (string)listBox1.Items[i]; Jan 19, 2010 at 20:23
  • This won't work - he's removing items, so after items[5] is removed, items[6] will not point to the next item in the original list. Instead, you have to iterate from the end to the beginning.
    – egrunin
    Apr 1, 2015 at 5:31
  • @egrugin, that's EXACTLY what I said in my "edit2" comment. Iterating backwards is just another way to address the issue. Apr 5, 2015 at 17:41
0

Sorry guys i had to adjust the string

sqldatapull = dr[0].ToString();
                if (sqldatapull.StartsWith("OBJECT"))
                {
                    sqldatapull = "";
                }
                listBox1.Items.Add(sqldatapull);
                for (int i = listBox1.Items.Count - 1; i >= 0; i--)
                {
                    if (String.IsNullOrEmpty(listBox1.Items[i] as String))
                        listBox1.Items.RemoveAt(i);
                }
            }
0

Your question implies that you're willing to modify other parts of the code, even though you can't modify the SQL Statement itself. Instead of removing them from the ListBox Collection, it might be easier to just exclude them in the first place. This code assumes you're connecting to SQL Server:

void PopulateListBox(ListBox listToPopulate)
{
    SqlConnection conn = new SqlConnection("myConnectionString"); 
    SqlCommand cmd = new SqlCommand("spMyStoredProc", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        string item = reader.GetString(0); //or whatever column is displayed in the list
        if (!item.Contains("OBJECT_"))
            listToPopulate.Items.Add(item); 
    }
}

But if you're absolutely determined to do it this way you should check out this question on modifying an enumerable collection while iterating through it.

0

You can try this also, if you don't want to deal with the enumerator:

object ItemToDelete = null;
foreach (object lsbItem in listbox1.Items)
{
   if (lsbItem.ToString() == "-ITEM-")
   {
      ItemToDelete = lsbItem;                                  
   }
}

if (ItemToDelete != null)
    listbox1.Items.Remove(ItemToDelete);
0

With this code you can remove every item from your listbox ... Notice that you should write this code in the click event of your button :

        if (listBox1.SelectedIndex != -1)
        {
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        } 
0

I found out the hard way that if your listbox items are assigned via a data source

List<String> workTables = hhsdbutils.GetWorkTableNames();
listBoxWork.DataSource = workTables;

...you have to unbind that before doing the removal:

listBoxWork.DataSource = null;
for (int i = listBoxWork.Items.Count - 1; i >= 0; --i)
{
    if (listBoxWork.Items[i].ToString().Contains(listboxVal))
    {
        listBoxWork.Items.RemoveAt(i);
    }
}

Without the "listBoxWork.DataSource = null;" line, I was getting, "Value does not fall within the expected range"

0
protected void lbAddtoDestination_Click(object sender, EventArgs e)
        {
            AddRemoveItemsListBox(lstSourceSkills, lstDestinationSkills);
        }
        protected void lbRemovefromDestination_Click(object sender, EventArgs e)
        {
            AddRemoveItemsListBox(lstDestinationSkills, lstSourceSkills);
        }
        private void AddRemoveItemsListBox(ListBox source, ListBox destination)
        {
            List<ListItem> toBeRemoved = new List<ListItem>();
            foreach (ListItem item in source.Items)
            {
                if (item.Selected)
                {
                    toBeRemoved.Add(item);
                    destination.Items.Add(item);
                }
            }
            foreach (ListItem item in toBeRemoved) source.Items.Remove(item);
        }
0

You can use following code too:

 foreach (var item in listBox1.Items.Cast<string>().ToList())
 {
     string removelistitem = "OBJECT";
     if (item.Contains(removelistitem))
     {
        listBox1.Items.Remove(item);
     }
 }
0
   for (int i = 0; i < listBox1.Items.Count; i++)
    {
        if (textBox1.Text == listBox1.Items[i].ToString())
        {
            jeElement = true;
            break;
        }
    }
    if (jeElement)
    {
        label1.Text = "je element";
    }
    else
    {
        label1.Text = "ni element";
    }
    textBox1.ResetText();
    textBox1.Focus();

}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Alt == true && e.KeyCode == Keys.A)
    {
        buttonCheck.PerformClick();
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.