循环遍历Windows窗体中的所有图表

时间:2015-12-18 23:04:02

标签: c# visual-studio foreach charts

我想使用Visual Studio C#2013在循环中访问我的Windows窗体中的所有图表。我在表单中有一堆它们,我想迭代地向它们添加新系列,具体取决于chart.name < / p>

我试过的是使用foreach循环和controls.OfType来循环它们。但是编译器给了我一个错误。

        foreach (Chart tempchart in this.Controls.OfType<Chart>)
        {
            MessageBox.Show(tempchart.Name);
        }
  

错误:   Foreach无法在“方法组”上运行。您打算调用'方法组'吗?*

我在这里做错了什么?我试图搜索图表集合等,但是很多关于如何将数据集合和系列集合添加到图表的答案。

1 个答案:

答案 0 :(得分:1)

您尝试循环方法本身而不是方法结果。

var method = this.Controls.OfType<Chart>;
var methodResult = this.Controls.OfType<Chart>();

您只会错过调用方法的最后()

foreach ( Chart tempchart in this.Controls.OfType<Chart>() )
{
  MessageBox.Show( tempchart.Name );
}