博文主要以下图中的控件来比较这两种方式获取控件的方式:
1. 最简单的方式:
private void GetControls1(Control fatherControl)
{
Control.ControlCollection sonControls = fatherControl.Controls;
//遍历所有控件
foreach (Control control in sonControls)
{
listBox1.Items.Add(control.Name);
}
}
结果:
获取的结果显示在右侧的ListBox中,可以发现没有获取到Panel、GroupBox、TabControl等控件中的子控件。