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

  • 相关阅读:
    《如何快速阅读一本书》读书笔记总结(实测,对提升阅读速度和质量非常有用)---2020年的第8/100本 (一目十行读书训练法)
    穷查理宝典 读书笔记--2020年的第1/100本
    200个查理芒格思维模型---多元思维模型
    lintcode算法周竞赛
    2016 Google code jam 大赛
    程序员面试心得总结
    (lintcode全部题目解答之)(附容易犯的错误)
    模板
    《非连续性》 混沌大学商学院第3章 读书笔记总结--2020年的第19/100本
    《第二曲线》 混沌大学商学院第2章 读书笔记总结--2020年的第18/100本
  • 原文地址:https://www.cnblogs.com/jacker1979/p/1716072.html
Copyright © 2011-2022 走看看