-1

I am getting

System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.

every time I want to remove an item from the array, so I could draw (fill) another rectangle at that place.

Everything is happening on MouseDown event on right click black rectangle should be drawn (filled) and on left white one.

I made 2 lists of rectangles, one for white ones, and one for black ones.

I am getting exception thrown when I try to draw (fill) rectangle of opposite color of the one that is.

Everything is written into a bitmap and later bitmap is draw as an image in Paint event.

 private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
        if (e.Button == MouseButtons.Right)
        {
                using (Graphics rectGraphics = Graphics.FromImage(rectBitmap))
                {
                    rBlack = new Rectangle((e.X / 20) * 20, (e.Y / 20) * 20, 20, 20);

                    rectGraphics.SmoothingMode = SmoothingMode.HighSpeed;


                    foreach (Rectangle r in whiteRectangles) // place where exception is thrown if I want to fill black rectangle on the place where white is
                    {
                            if (r.X - 1 == rBlack.X && r.Y - 1 == rBlack.Y)
                            {
                                int index = whiteRectangles.IndexOf(r);
                                whiteRectangles.RemoveAt(index);
                            }

                            rectGraphics.FillRectangle(brushWhite, r);
                    }

                    blackRectangles.Add(rBlack);

                    foreach (Rectangle r in blackRectangles)
                    {
                        rectGraphics.FillRectangle(brushBlack, r);
                    }
              }
        }

        if (e.Button == MouseButtons.Left)
        {
                using (Graphics rectGraphics = Graphics.FromImage(rectBitmap))
                {
                    rWhite = new Rectangle((e.X / 20) * 20 +1, (e.Y / 20) * 20 +1, 19, 19);
                    rectGraphics.SmoothingMode = SmoothingMode.HighSpeed;

                    foreach (Rectangle r in blackRectangles) // place where exception is thrown if I try to fill white rectangle on the place where black is
                    {
                            if (r.X + 1 == rWhite.X && r.Y + 1 == rWhite.Y)
                            {
                                int index = blackRectangles.IndexOf(r);
                                blackRectangles.RemoveAt(index);
                            }

                            rectGraphics.FillRectangle(brushBlack, r);
                    }

                    whiteRectangles.Add(rWhite);

                    foreach (Rectangle r in whiteRectangles)
                    {
                        rectGraphics.FillRectangle(brushWhite, r);
                    }
              }
        }

        this.Refresh();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LordRed
  • 31
  • 4
  • 1
    Possible duplicate of [what is the most performant way to delete elements from a List in c# while doing a foreach?](https://stackoverflow.com/questions/9331014/what-is-the-most-performant-way-to-delete-elements-from-a-listt-in-c-sharp-whi) – Phil M Mar 02 '19 at 01:56
  • Thanks , not getting exception anymore, but I feel like it didn't do anything, rectangles now change colors but they are not removed from list of color that was there before. Am I supposed to put this for loop answer from that thread in foreach loop? Is there other way to do this? – LordRed Mar 02 '19 at 02:07

1 Answers1

0

You probably need to use a for loop instead of a foreach. Set your upper limit using the Count of the list, then delete by index.

for (int i = 0; i < list.Count; i++)
{
 //Delete list[i] here
}

Or, in reverse

for (int i = list.Count - 1; i >= 0; i--)
{
 //Delete list[i] here
}
  • I used it in foreach loop so I could compare values of rectangles of opposite color to the one I want to draw , I don't think I can get that what rectangle is where I clicked so i can delete him... I already tried both for loops in foreach same result. – LordRed Mar 02 '19 at 02:29