遍历窗口中所有控件及ContextMenuStrip、Timer控件

遍历窗口中所有控件及ContextMenuStrip、Timer控件2009-06-19 遍历窗口中所有控件 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;
}
}



posted @ 2009-07-22 16:33  Cad人生  阅读(534)  评论(0编辑  收藏  举报