C# winforms,遍历组件

C# winforms,遍历组件,c#,winforms,C#,Winforms,我试图在表单上遍历组件,但组件似乎为空。 那么,如何循环表单上的组件(而不是控件) public partial class FormBase : Form { public FormBase() { InitializeComponent(); FixVisualDesignerIssues(); } protected void FixVisualDesignerIssues() { // this.c

我试图在表单上遍历组件,但组件似乎为空。 那么,如何循环表单上的组件(而不是控件)

public partial class FormBase : Form
{
    public FormBase()
    {
        InitializeComponent();
        FixVisualDesignerIssues();
    }

    protected void FixVisualDesignerIssues()
    {
        // this.components is always NULL ????????
        foreach (var comp in this.components.Components.OfType<BindingSource>())
        {
            ((BindingSource)comp).do something, whatever
        }
    }

如果使用以下方法创建
BindingSource
,则它将包含在components容器中

BindingSource bindingSource1 = new BindingSource(components);

如果您正在使用任何其他方式创建绑定源,则
组件
容器中将不会有任何内容。

您可以显示元素gttDataTable的声明和初始化吗?
此。如果没有声明,组件
将无法编译。因此,请检查您的代码或粘贴您设置此指令的方式。我编辑了代码,使其使用bindingsource,这样人们就不会再询问我搜索的组件的声明。声明是什么并不重要,重要的是this.components为null。回答之前请先阅读问题。components编译得很好。components是winforms中Form类的属性,它不是我写的,而是microsoft写的。我只是想知道如何使用它bindingsource是通过将它放到设计器中的窗体上创建的。因此,我必须检查designer.cs以查看它是如何创建的?这是一个问题,我有一个基础表单,其他表单从中继承。在基本表单上没有组件,但是我想使用基本表单来循环通过继承表单放置在其上的所有组件。既然继承的表单最终也将调用InitialiseComponents,那么它应该能够在那个时候找到所有组件吗?这就是我在Delphi中如何做到的,如何在c#中做到这一点?你能分享一下显示表单结构的代码吗?请注意,组件集合和bindingsource都是私有对象。好吧,baseform的整个代码都在问题中,这里没有更多内容。所有其他表单都继承自此表单,并使用可视化设计器组件(如bindingsource)和控件(如TextBox和DataGridView)将其放在其中。您无法从基本表单访问继承的表单组件。
BindingSource bindingSource1 = new BindingSource(components);