獲得控件的名稱

在.net智能設備程序開發中Control 沒有Name屬性,下面是一個能夠獲得控件名稱的方法

/// <summary>
  /// 獲得控件的名稱
  /// </summary>
  /// <param name="control">控件</param>
  /// <returns>控件名稱</returns>
  private static string GetControlName(System.Windows.Forms.Control control)
  {
   Control ptrol=control.Parent;
   //當控件爲頂級窗體時
   if(ptrol == null)
   {
    Type t1= control.GetType();
    return t1.FullName.Substring(t1.FullName.LastIndexOf(".")+1);
    
   }
   //獲得控件名稱ss
   Type t=control.Parent.GetType();
   System.Reflection.FieldInfo[] fi=t.GetFields(System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.IgnoreCase);
   foreach(System.Reflection.FieldInfo f in fi)
   {
     string bb=f.Name;
     object obj=f.GetValue(control.Parent);
     if(obj!=null)
      if(f.GetValue(control.Parent).Equals(control))
       return f.Name;
    
   }
   return null;
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章