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 9336037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T07:26:10+00:00 2026-06-19T07:26:10+00:00

I have a foreach loop inside of an button-click event handler that I’m using

  • 0

I have a foreach loop inside of an button-click event handler that I’m using to get rid of controls that all sit on the same point on the Y axis. I have a control that adds a row, and then I need users to have the option to remove a row they didn’t mean to add.

When I use the foreach loop by itself, it consistently misses two items–the same two every time. I’ve confirmed that the Y values are in fact identical. If I keep running the function, it removes all the controls on the row after three runs. However, if I nest the foreach loop, using all the same values, it works. See the code below.

This seems like a pretty sloppy way to do it, but it’s also the only solution that has worked for me. I saw a couple other posts that said to declare a second variable within the loop (i.e., Control z = c; if(z.Location....)). That didn’t make any difference in the behavior here. Can anyone explain why the foreach loop by itself isn’t working? How do I fix it without the repetitive nesting?

Working:

    internal void MinButt_Click(object sender, EventArgs e)
    {
        Scratch.tScratch.panel2.Controls.RemoveByKey("Record" + arrDynamY[0].ToString());
        foreach (Control c in Scratch.tScratch.panel2.Controls)
        {
            if (c.Location.Y == arrDynamY[1])
            {
                c.Dispose();
            }

            foreach (Control ctrl in Scratch.tScratch.panel2.Controls)
            {
                if (ctrl.Location.Y == arrDynamY[1])
                {
                    ctrl.Dispose();
                }
            }
        }
    }

Misses the same two controls every time:

    internal void MinButt_Click(object sender, EventArgs e)
    {
        Scratch.tScratch.panel2.Controls.RemoveByKey("Record" + arrDynamY[0].ToString());
        foreach (Control c in Scratch.tScratch.panel2.Controls)
        {
            if (c.Location.Y == arrDynamY[1])
            {
                c.Dispose();
            }
        }
    }
  • 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-06-19T07:26:11+00:00Added an answer on June 19, 2026 at 7:26 am

    You should firstly obtain controls for removing and remove them afterwards like this:

    var controlsToRemove = from Control c in Scratch.tScratch.panel2.Controls
                           where c.Location.Y == arrDynamY[1]
                           select c;
    
    foreach (var c in controlsToRemove.ToArray())
    {
        Scratch.tScratch.panel2.Controls.Remove(c);
        c.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a foreach loop and inside the loop I
i want to have a foreach loop where the initial array is changed inside
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
Right now I have a foreach loop that grabs the first link with an
in a ASP.NET application (MVC) I have a foreach loop that loops through a
Im using MVC-Viewmodel, EF Model first on my project. I have a foreach loop
I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
I have a foreach loop inside of an if statement. My code is in
I have a foreach loop that I am parallelizing and I noticed something odd.
i have 2 divs inside foreach loop .first div has information about a company

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.