本文实例讲述了WinForm遍历窗体所有子控件的方法。分享给大家供大家参考,具体如下:
////// C# 只遍历控件子控件,不遍历孙控件 ///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件 /// ///控件类型 /// 要遍历的控件 /// 控件名 ///public static T GetControl (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 (_control, controlsName); if (_control != null) return (T)_control; } } return null; } /// /// 遍历窗体所有子控件 /// ///控件类型 /// 窗体名 /// 控件名 ///public static T GetControl (Form form, string controlsName) where T : Control { T _Control = null; for (int i = 0; i < form.Controls.Count; i++) { _Control = GetControl (form.Controls[i], controlsName); if (_Control != null) return _Control; } return null; }
更多关于C#相关内容感兴趣的读者可查看本站专题:《WinForm控件用法总结》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。



