如何在c#中创建多个自定义控件的集合 [英] how to make a collection of multiple Custom control in c#

查看:482
本文介绍了如何在c#中创建多个自定义控件的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Windows应用程序中创建了3个自定义控件,我想在其他页面中使用它们,所以如何在c#中的其他页面上创建这些控件的集合..

i want在一个循环中调用所有控件..

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..

推荐答案

要在循环中使用它们,它们必须是集合的一部分 - 所以要么你需要插入它们进入您创建的集合,或使用表单的控件集合(或您添加到其中的其他控件)来定位它们。



使用您自己的集合,这是微不足道的,但是控件收集需要更多的关注:

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:
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.


这篇关于如何在c#中创建多个自定义控件的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆