问题描述
所以我有一个相对简单的任务,在表格
上循环遍历所有 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?
推荐答案
这篇关于通过继承表单中的控件循环...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!