通过继承表单中的控件循环... [英] Looping through Controls in inherited Form...

查看:100
本文介绍了通过继承表单中的控件循环...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个相对简单的任务,在表格上循环遍历所有 Controls 之前已经多次做过没有麻烦。这次我确实遇到了问题。 表单我传递给我的函数有一个基本的表单已经有一些控制就可以了。现在问题是我的函数只遍历基础表单上的 Controls ,忽略所有控件我添加到当前的表单

我正在使用的函数循环遍历<我的表格上的code>控件如下所示:

So I''m having a relatively simple task of looping through all Controls on a Form, something I''ve done many times before without troubles. This time I do have a problem though. The Form I''m passing to my function has a base Form which already has some Controls on it. Now the problem is that my function only loops through the Controls on the base Form, ignoring all the Controls I added to the current Form.
The function I''m using to loop through the Controls on my Form looks like this:

public void LoopThroughControls(Control parent)
{
   if (parent != null)
   {
      foreach (Control ctrl in parent.Controls)
      {
         this.LoopThroughControls(ctrl);
         // Do something with Control here...
      }
   }
}

我在组件中使用它,我得到 Form 如下(从 System.Windows.Forms.ErrorProvider网站属性复制):

I use this from within a Component and I get my Form as follows (copied from the System.Windows.Forms.ErrorProvider Site Property):

public override ISite Site
{
   get { return base.Site; }
   set
   {
      base.Site = value;
      if ((value != null))
      {
         // Try to get the IDesignerHost (the Form of the current Component)
         IDesignerHost host = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
         if ((host != null))
         {
            // With the IDesignerHost it is possible to get the RootComponent.
            // This is the Form for the active designer.
            IComponent rootComponent = host.RootComponent;
            if (rootComponent is ContainerControl)
            {
               // If the RootComponent is a ContainerControl we assign it to the ContainerControl Property.
               this.ContainerControl = (ContainerControl)rootComponent;
            }
         }
      }
   }
}

然后我调用 LoopThroughControls 函数传入 ContainerControl 作为参数。当我设置断点时,我可以在 ContainerControl 中看到所有控件,但在循环遍历<$时会忽略它们c $ c>控件。

当我直接从我的派生表格调用上述函数时,DOES循环遍历所有控制。但是, ContainerControl 似乎也引用了派生的表单,所以我真的看不到差异......

所以任何人都有解释,甚至更好的解决方案?

I then call the LoopThroughControls function passing in ContainerControl as parameter. When I set a breakpoint I can see all the Controls in ContainerControl, but they are ignored when looping through the Controls.
When I call the above function directly from my derived Form is DOES loop through ALL Controls. However, ContainerControl seems to have a reference to the derived Form as well, so I can''t really see the difference...
So anyone has an explanation, and even better, a solution?

推荐答案

我找到了自己的解决方案。比预期还要早(虽然它仍然花了我一整个上午......)。

我所看到的是我的表格,所有TextBox字段,按钮字段等字段。

但是,它们还没有放在我的表格上,所以它们还没有被添加到表格的控件中。

我实现了 ISupportInitialize 并在 EndInit 中调用该函数。按预期工作。
I found my own solution to this one. Sooner than expected too (although it still cost me an entire morning...).
What I was looking at were the fields of my Form, all TextBox fields, Button fields, etc.
However, they weren''t placed on my Form yet, so they were also not yet added to the Controls of the Form.
I implemented ISupportInitialize and called the function at EndInit. Works as expected now.


这篇关于通过继承表单中的控件循环...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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