亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

WinForm實(shí)現(xiàn)按名稱遞歸查找控件的方法

 更新時間:2014年08月19日 17:29:23   投稿:shichen2014  
這篇文章主要介紹了WinForm實(shí)現(xiàn)按名稱遞歸查找控件的方法,需要的朋友可以參考下

本文所述實(shí)例主要實(shí)現(xiàn)了WinForm實(shí)現(xiàn)按名稱遞歸查找控件的功能,在C#項(xiàng)目開發(fā)中有一定的應(yīng)用價值,分享給大家供大家參考借鑒。

關(guān)鍵代碼如下:

/// <summary>
/// 向下遞歸查找控件
/// </summary>
/// <param name="parentControl">查找控件的父容器控件</param>
/// <param name="findCtrlName">查找控件名稱</param>
/// <returns>若沒有查找到返回NULL</returns>
public static Control DownRecursiveFindControl(this Control parentControl, string findCtrlName)
{
  Control _findedControl = null;
  if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)
  {
 foreach (Control ctrl in parentControl.Controls)
 {
   if (ctrl.Name.Equals(findCtrlName))
   {
 _findedControl = ctrl;
 break;
   }
   else
   {
 if (ctrl.Controls.Count > 0)
   _findedControl = DownRecursiveFindControl(ctrl, findCtrlName);
   }
 }
  }
  return _findedControl;
}
/// <summary>
/// 將Control轉(zhuǎn)換某種控件類型
/// </summary>
/// <typeparam name="T">控件類型</typeparam>
/// <param name="control">Control</param>
/// <param name="result">轉(zhuǎn)換結(jié)果</param>
/// <returns>若成功則返回控件;若失敗則返回NULL</returns>
public static T Cast<T>(this Control control, out bool result) where T : Control
{
  result = false;
  T _castCtrl = null;
  if (control != null)
  {
 if (control is T)
 {
   try
   {
 _castCtrl = control as T;
 result = true;
   }
   catch (Exception ex)
   {
 Debug.WriteLine(string.Format("將Control轉(zhuǎn)換某種控件類型異常,原因:{0}", ex.Message));
 result = false;
   }
 }
  }
  return _castCtrl;
}

測試代碼如下:

bool _sucess = false;
CheckBox _finded = this.DownRecursiveFindControl("checkBox1").Cast<CheckBox>(out _sucess);
if (_sucess)
{
 MessageBox.Show(_finded.Name);
}
else
{
 MessageBox.Show("Not Finded.");
}

希望本文所述實(shí)例能夠?qū)Υ蠹业腃#程序設(shè)計(jì)有所幫助!

相關(guān)文章

最新評論