Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 701219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:35:07+00:00 2026-05-14T03:35:07+00:00

I had learnt by reading your great answers here, that it is not good

  • 0

I had learnt by reading your great answers here, that it is not good practice deleting items from within a foreach loop, as it is (and I quote) “Sawing off the branch you’re sitting on”.

My code currently removes the text from the dropdownlist, but the actual item remains (just without text displayed).

In other words, it isn’t deleting, and probably can’t because you can’t delete from within a foreach loop.

After hours of trying I am unable to get my head around a way of doing it.

//For each checked box, run the delete code
for (int i = 0; i < this.organizeFav.CheckedItems.Count; i++)
{
    //this is the foreach loop
    foreach (ToolStripItem mItem in favoritesToolStripMenuItem.DropDownItems)
    {
        //This rules out seperators
        if (mItem is ToolStripMenuItem)
        {
            ToolStripMenuItem menuItem = mItem as ToolStripMenuItem;

            //This matches the dropdownitems text to the CheckedItems String
            if (((ToolStripMenuItem)mItem).Text.ToString() == organizeFav.CheckedItems[i].ToString())
            {
                //And deletes the item
                menuItem.DropDownItems.Remove(mItem);
            }
        }
    }
}

But it isn’t deleting because it is within a foreach loop!
I would greatly appreciate your help, and be truly amazed if anyone can get their head around this code 🙂

Kind Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:35:08+00:00Added an answer on May 14, 2026 at 3:35 am

    You don’t need a foreach loop – just use a regular loop but go in reverse, start at the end and go to the beginning.

    //For each checked box, run the delete code
    for (int i = 0; i < this.organizeFav.CheckedItems.Count; i++)
    {
        //this *replaces* the foreach loop
        for(int j = favoritesToolStripMenuItem.DropDownItems.Count - 1; j >= 0; j--)
        {
            ToolStripMenuItem menuItem = favoritesToolStripMenuItem.DropDownItems[j] as ToolStripMenuItem; 
    
            //This rules out seperators
            if (menuItem != null)
            {
                //This matches the dropdownitems text to the CheckedItems String
                if (menuItem.Text.ToString() == organizeFav.CheckedItems[i].ToString())
                {
                    favoritesToolStripMenuItem.DropDownItems.Remove(menuItem);
                }
            }
        }
     }
    

    this was @Kurresmack’s code rearranged, i just coded it directly here in the page so excuse any small syntax error or anything obvious i overlooked (disclaimer: it is a sample!!)

    You can still treat favoritesToolStripMenuItem.DropDownItems as a collection like you were, but you don’t have to enumerate over it using a foreach. This cuts down on a few lines of code, and it works because you are iterating it in reverse order, you will not get an index out of bounds exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a good search here but can't see anything that gets my mind in
I was just reading this: http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx And had some questions about some of the
Had a page that was working fine. Only change I made was to add
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I've been using J for a few months now, and I find that reading
I am a C programmer, but had learnt C++ @school longtime back. Now I
I have had an issue with any fonts I have embedded not showing up
Hey this is one of the classes that I had for homework in my
1st of all: I'm not a programmer, never learnt programming/algorithms. Actually I have to
I'm reading through the Big Nerd Ranch book on iOS programming and I had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.