显示列表

遍历窗口中所有控件
foreach (Control c1 in this.Controls)
所有控件都在集合 Controls 中。
但 ContextMenuStrip、Timer 等控件不在这个集合中,如果要获取可以使用反射的方法

System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
for (int i = 0; i < fieldInfo.Length; i++)
{
   switch (fieldInfo[i].FieldType.Name)
   {
       case "ContextMenuStrip":
           ContextMenuStrip contextMenuStrip = (ContextMenuStrip)fieldInfo[i].GetValue(this);
           MessageBox.Show(contextMenuStrip.Name);
           break;
       case "Timer":
           Timer timer = (Timer)fieldInfo[i].GetValue(this);
           MessageBox.Show(timer.Interval.ToString());
           break;
   }
}

返回摘要 | 分类(C#/CSharp) | 访问(1) | 编辑