public void TraverControl(Control Ctl)
{
foreach (Control c in Ctl.Controls)
{
label1.Text += "
" + "" + c.Name + "" + "
";
//用于显示窗体中包含的所有的控件名,首先显示的是最外层的控件
if (c.Controls.Count == 0)
{
continue;
}
else
{
Control C = c;
TraverControl(C); //递归调用
}
}
}
private void button1_Click(object sender, EventArgs e)
{
TraverControl(this);//调用递归函数
}