Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i created 3 custom control in my windows application, and i want to use them in other page ,, so how can i make a collection of those controls on other page in c#..
i want to call all controls in a single loop..
Posted

1 solution

To use them in a loop, they have to be part of a collection - so either you need to insert them into a collection you create, or use the Controls collection of the form (or other control you added them to) to locate them.

With your own collection that is trivial, but teh Control collection needs more care:
C#
foreach (Control c in Controls)
   {
   MyUserControl uc = c as MyUserControl;
   if (uc != null)
      {
      Console.WriteLine(um.MyProperty);
      }
   }
If the three controls are not all the same class, then you either need to check for all three, or for the base class for all three.

Note that if the user controls are inside a container such as a panel, you need to explicitly check that controls Controls collection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900