private int FindLabelCount()
{
return FindLabelCount(this);
}
private int FindLabelCount(Control control)
{
int count = 0;
if (control.HasChildren)
{
foreach (Control child in control.Controls)
{
if (child is Label)
{
count++;
}
count += FindLabelCount(child);
}
}
return count;
}
使用int count = FindLabelCount();就可以获取
foreach(Control ctl in this.Controls)
{
if(ctl is Label)
//找到Label控件了。
}