zoukankan      html  css  js  c++  java
  • WinForm开发中几种找控件的方法

    #region 在当前控件集中查找ToolStripButton
            private static ToolStripButton FindToolStripButton(Form owner, string controlId)
            {
                ToolStripButton result;
                result = null;
                foreach (Control ctl in owner.Controls)
                {
                    if (ctl.GetType() == typeof(System.Windows.Forms.ToolStrip))
                    {
                        ToolStrip ts = ctl as ToolStrip;
                        ToolStripItem[] items = ts.Items.Find(controlId, true);
                        if (items.Length > 0)
                        {
                            result = (ToolStripButton)items[0];
                            return result;
                        }
                    }
                }
                return result;
            }
            #endregion

    //Form中直接找

    Control[] ctls = owner.Controls.Find(dr["ControlName"].ToString(), true);

    //Form的Components中找控件

     ArrayList myList = owner.GetIntfObjects(typeof(IFindContainer));
    for (int i = 0; i < myList.Count; i++)
    {

      if(myList[i] is DataSet)

      {}

    }

    public ArrayList GetIntfObjects(Type intfType)
            {
                ArrayList aRet = new ArrayList();

                ReflectionPermission reflectionPerm1 = new ReflectionPermission(PermissionState.None);
                reflectionPerm1.Flags = ReflectionPermissionFlag.AllFlags;

                if (intfType.IsInterface)
                {
                    Type type = this.GetType();
                    FieldInfo[] myFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                    for (int i = 0; i < myFields.Length; i++)
                    {
                        object newobj = myFields[i].GetValue(this);
                        if ((null != newobj) && (null != newobj.GetType().GetInterface(intfType.Name)))
                        {
                            aRet.Add(newobj);
                        }
                    }
                }
                return aRet;
            }

  • 相关阅读:
    关于正则表达式的递归匹配问题
    给程序添加启动画面
    C#中的ICollection接口
    C#基本线程同步
    C# 图片裁剪代码
    .NET程序性能的基本要领
    C# 6与VB 12即将加入模式匹配
    Python实例---利用正则实现计算器[FTL版]
    Python实例---利用正则实现计算器[参考版]
    Python学习---重点模块之subprocess
  • 原文地址:https://www.cnblogs.com/jacker1979/p/1716072.html
Copyright © 2011-2022 走看看