?
Solved

Code to loop through controls on a form

Posted on 2004-11-17
5
Medium Priority
?
274 Views
Last Modified: 2010-04-15
In C# I need some code that allows me to loop through controls on a form and Remove controls of type PictureBox.
0
Comment
Question by:Tom Knowlton
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 2000 total points
ID: 12608059
           private void button1_Click(object sender, System.EventArgs e)
            {
                  for(int i=Controls.Count-1;i>0; i--)
                  {
                        if(Controls[i].GetType().ToString() == "System.Windows.Forms.PictureBox")
                        {
                              Controls.Remove(Controls[i]);
                        }
                  }
            }
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12608095
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12608106
Another solution:

         public void ClearGameBoard()
          {
               foreach (Control c in this.Controls)
               {
                    if (c is System.Windows.Forms.PictureBox)
                    {
                         System.Windows.Forms.PictureBox pb = (System.Windows.Forms.PictureBox) c;
                         this.Controls.Remove(pb);
                    }                    
               }
          }
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12608141
Ah nice, I like your solution better!  I should give you points.  ;-)

I'll look at your other question after lunch...

Cheers,
Aaron
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12608163
Thanks.....I actually found my alternative solution on EE as well.  Sometimes I forget to search first....then ask.  :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question