zoukankan      html  css  js  c++  java
  • window forms遍历窗体所有控件

    /// <summary>

            /// 只遍历控件的子控件,不遍历孙控件

            ///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件

            /// </summary>

            /// <typeparam name="T">要匹配的控件类型</typeparam>

            /// <param name="control">要遍历的了控件</param>

            /// <param name="controlsName">要匹配的控件名</param>

            /// <returns></returns>

            public static Control GetControl<T>(Control control, string controlsName)

            {

                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 _control;

                    if (_control.HasChildren)

                    {

                        _control = GetControl<T>(_control, controlsName);

                        if (_control != null)

                            return _control;

                    }

                }

                return null;

            }

            /// <summary>

            /// 遍历窗体所有控件

            /// </summary>

            /// <typeparam name="T">要匹配的控件类型</typeparam>

            /// <param name="form">窗体名</param>

            /// <param name="controlsName">要匹配的控件名</param>

            /// <returns></returns>

            public static Control GetControl<T>(Form form, string controlsName)

            {

                Control _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;

            }

    使用方法:

    Control _control;

     _control = GetControl<TextBox>(customer, "txtValue");

        if(_control!=null)

               ((TextBox) _control).Text = "text";

     _control = GetControl<ComboBox>(customer, "ddl");

        if (_control != null)

                ((ComboBox)_control).SelectedIndex = 0;

  • 相关阅读:
    20位活跃在Github上的国内技术大牛
    ubuntu下安装ros出现“无法下载-package.ros.org中某个包-校验和不符”的解决方法
    从ROS bag文件中提取图像
    计算机视觉、机器学习相关领域论文和源代码大集合
    使用XV-11激光雷达做hector_slam
    机器人操作系统(ROS)教程22:ROS的3D可视化工具—rviz
    ROS探索总结(三)——ROS新手教程
    bootstrap文件上传C#实现
    .net分流抢票助手
    谷歌浏览器整个网页截图方法
  • 原文地址:https://www.cnblogs.com/huangtailang/p/2220127.html
Copyright © 2011-2022 走看看