473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C# App: "Foreach" doesn't seem to loop through all controls on form

AHayes
10
_Background
I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think) remove these items, but I noticed that not all of my buttons are getting removed from the screen.

_The Code
This is a sample of how I'm CREATING the buttons:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < arrButtonData.GetLength(0); i++)
  2. {
  3.         //Make new button               
  4.             Button btn_DynModify = new Button();
  5.  
  6.             //Initialize data.
  7.             btn_DynModify.Location = pntCurrentLoc;
  8.             btn_DynModify.Name = "btn_Modify_" + arrButtonData[i, 1];
  9.             btn_DynModify.BackColor = System.Drawing.Color.FromArgb(231, 241, 255);
  10.             btn_DynModify.Cursor = Cursors.Hand;
  11.             btn_DynModify.Width = 90;
  12.             btn_DynModify.Height = 90;
  13.             btn_DynModify.FlatStyle = FlatStyle.Flat;
  14.             btn_DynModify.FlatAppearance.BorderSize = 0;
  15.             btn_DynModify.Click += new System.EventHandler(btn_DynModify_Click);
  16.             btn_DynModify.Text = arrButtonData[i, 0];
  17.             btn_DynModify.TextAlign = ContentAlignment.BottomCenter;
  18.  
  19.             //Update location                
  20.             if (intCounter % intMaxCol == 0)
  21.             {
  22.                 pntCurrentLoc.X -= (btn_DynModify.Width + intOffsetX) * (intMaxCol - 1);
  23.                         pntCurrentLoc.Y += btn_DynModify.Height + intOffsetY;
  24.             }
  25.             else
  26.             {
  27.                         pntCurrentLoc.X += btn_DynModify.Width + intOffsetX;
  28.             }
  29.  
  30.  
  31.             //Add button to form and bring it to the front of the z-order stack.
  32.             Controls.Add(btn_DynModify);
  33.             btn_DynModify.BringToFront();
  34. }
  35.  
And this is how I'm attempting to remove the buttons:

Expand|Select|Wrap|Line Numbers
  1. //Clear pre-existing menu items.
  2. foreach (Control ctl in Controls)
  3. {
  4.     if (ctl.Name.StartsWith("btn_Modify_"))
  5.     {
  6.         Controls.Remove(ctl);
  7.     }
  8. }
  9.  

_What SEEMS to Be the Problem
I stepped through my code, specifically keeping an eye on the "foreach" loop each time. My assumption on what's going on is that it's not looping through ALL controls on the page, only some of them (seems to be only half of the buttons I want "removed" on screen are actually being removed when I call the "foreach" loop.


Anybody know why this might be happening?
Oct 3 '07 #1
3 11338
r035198x
13,262 8TB
_Background
I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think) remove these items, but I noticed that not all of my buttons are getting removed from the screen.

_The Code
This is a sample of how I'm CREATING the buttons:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < arrButtonData.GetLength(0); i++)
  2. {
  3.         //Make new button               
  4.             Button btn_DynModify = new Button();
  5.  
  6.             //Initialize data.
  7.             btn_DynModify.Location = pntCurrentLoc;
  8.             btn_DynModify.Name = "btn_Modify_" + arrButtonData[i, 1];
  9.             btn_DynModify.BackColor = System.Drawing.Color.FromArgb(231, 241, 255);
  10.             btn_DynModify.Cursor = Cursors.Hand;
  11.             btn_DynModify.Width = 90;
  12.             btn_DynModify.Height = 90;
  13.             btn_DynModify.FlatStyle = FlatStyle.Flat;
  14.             btn_DynModify.FlatAppearance.BorderSize = 0;
  15.             btn_DynModify.Click += new System.EventHandler(btn_DynModify_Click);
  16.             btn_DynModify.Text = arrButtonData[i, 0];
  17.             btn_DynModify.TextAlign = ContentAlignment.BottomCenter;
  18.  
  19.             //Update location                
  20.             if (intCounter % intMaxCol == 0)
  21.             {
  22.                 pntCurrentLoc.X -= (btn_DynModify.Width + intOffsetX) * (intMaxCol - 1);
  23.                         pntCurrentLoc.Y += btn_DynModify.Height + intOffsetY;
  24.             }
  25.             else
  26.             {
  27.                         pntCurrentLoc.X += btn_DynModify.Width + intOffsetX;
  28.             }
  29.  
  30.  
  31.             //Add button to form and bring it to the front of the z-order stack.
  32.             Controls.Add(btn_DynModify);
  33.             btn_DynModify.BringToFront();
  34. }
  35.  
And this is how I'm attempting to remove the buttons:

Expand|Select|Wrap|Line Numbers
  1. //Clear pre-existing menu items.
  2. foreach (Control ctl in Controls)
  3. {
  4.     if (ctl.Name.StartsWith("btn_Modify_"))
  5.     {
  6.         Controls.Remove(ctl);
  7.     }
  8. }
  9.  

_What SEEMS to Be the Problem
I stepped through my code, specifically keeping an eye on the "foreach" loop each time. My assumption on what's going on is that it's not looping through ALL controls on the page, only some of them (seems to be only half of the buttons I want "removed" on screen are actually being removed when I call the "foreach" loop.


Anybody know why this might be happening?
The foreach loop works by iterating over a collection. You cannot modify a collection while you are iterating over it at the same time. You should convert Controls ToArray and then modify that array.
Oct 3 '07 #2
AHayes
10
The foreach loop works by iterating over a collection. You cannot modify a collection while you are iterating over it at the same time. You should convert Controls ToArray and then modify that array.

So, if I understand you correctly, the problem is occurring because I'm removing parts of the collection while trying to read it...which is causing it to "skip" over some of them.


That makes sense. Thanks.
Oct 3 '07 #3
Plater
7,872 Expert 4TB
There is also a situation (at least in windows applications) where you would need to look at CHILD controls of the controls in Page.Controls.
like if you put a textbox in a groupbox and then put the groupbox on the page, you would only get the groupbox in the iteration, you would need to iterate through child controls of the groupbox to find the textbox.
Oct 3 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.