WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件
更新時(shí)間:2014年08月19日 17:25:29 投稿:shichen2014
這篇文章主要介紹了WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件,需要的朋友可以參考下
本文所述實(shí)例實(shí)現(xiàn)WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件的功能,在C#程序開發(fā)中有一定的實(shí)用價(jià)值。分享給大家供大家參考。
關(guān)鍵代碼如下:
/// <summary> /// 按名稱查找控件 /// </summary> /// <param name="parentControl">查找控件的父容器控件</param> /// <param name="findCtrlName">查找控件名稱</param> /// <returns>若沒有查找到返回NULL</returns> public static Control FindControl(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; } } } 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 = panel1.FindControl("checkBox1").Cast<CheckBox>(out _sucess); if (_sucess) { MessageBox.Show(_finded.Name); } else { MessageBox.Show("Not Finded."); }
希望本文實(shí)例對大家C#學(xué)習(xí)能有所幫助!
您可能感興趣的文章:
- c# Winform自定義控件-儀表盤功能
- winform實(shí)現(xiàn)可拖動(dòng)的自定義Label控件
- C#中Winform 實(shí)現(xiàn)Ajax效果自定義按鈕
- C# winform自定義翻頁控件詳解
- WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- C# Winform使用擴(kuò)展方法實(shí)現(xiàn)自定義富文本框(RichTextBox)字體顏色
- C# WinForm中實(shí)現(xiàn)快捷鍵自定義設(shè)置實(shí)例
- WinForm自定義控件應(yīng)用實(shí)例
- 解決C# winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法詳解
- .Net WInform開發(fā)筆記(三)談?wù)勛灾瓶丶?自定義控件)
- Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片
相關(guān)文章
C#設(shè)計(jì)模式之Builder生成器模式解決帶老婆配置電腦問題實(shí)例
這篇文章主要介紹了C#設(shè)計(jì)模式之Builder生成器模式解決帶老婆配置電腦問題,簡單介紹了生成器模式的概念、功能并結(jié)合具體實(shí)例形式分析了C#生成器模式解決配電腦問題的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09C#中的char、string和StringBuilder的使用詳解
這篇文章主要介紹了C#中的char、string和StringBuilder的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07C#?DateTime.Now.ToString()?用法示例講解
這篇文章主要介紹了C#?DateTime.Now.ToString()?用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01