WinForm遍歷窗體一切子控件的辦法。本站提示廣大學習愛好者:(WinForm遍歷窗體一切子控件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm遍歷窗體一切子控件的辦法正文
本文實例講述了WinForm遍歷窗體一切子控件的辦法。分享給年夜家供年夜家參考,詳細以下:
/// <summary> /// C# 只遍歷控件子控件,不遍歷孫控件 ///當控件有子控件時,須要用遞歸的辦法遍歷,能力全體列出控件上的控件 /// </summary> /// <typeparam name="T">控件類型</typeparam> /// <param name="control">要遍歷的控件</param> /// <param name="controlsName">控件名</param> /// <returns></returns> public static T GetControl<T>(Control control, string controlsName) where T : Control { if (control == null) return null; Control _control; for (int i = 0; i < control.Controls.Count; i++) { _control = control.Controls[i]; if (_control == null) return null; if (_control.Name == controlsName && _control is T) return (T)_control; if (_control.HasChildren) { _control = GetControl<T>(_control, controlsName); if (_control != null) return (T)_control; } } return null; } /// <summary> /// 遍歷窗體一切子控件 /// </summary> /// <typeparam name="T">控件類型</typeparam> /// <param name="form">窗體名</param> /// <param name="controlsName">控件名</param> /// <returns></returns> public static T GetControl<T>(Form form, string controlsName) where T : Control { T _Control = null; for (int i = 0; i < form.Controls.Count; i++) { _Control = GetControl<T>(form.Controls[i], controlsName); if (_Control != null) return _Control; } return null; }
願望本文所述對年夜家C#法式設計有所贊助。