Winform遍历所有控件

发布时间:2017-09-22编辑:佚名阅读(2185)

blob.png


        private void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            GetControls1(this); 
            GetControls2(this); 
        }
        private void GetControls1(Control fatherControl)
        {
            Control.ControlCollection sonControls = fatherControl.Controls;
            //遍历所有控件  
            foreach (Control control in sonControls)
            {
                listBox1.Items.Add(control.Name);
                //递归
                GetControls1(control);
            }
        }  
        private void GetControls2(Control fatherControl)
        {
            //反射  
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                listBox2.Items.Add(fieldInfo[i].Name);
            }
        }

blob.png

  关键字:Winform遍历所有控件


鼓掌

0

正能量

0

0

呵呵

0


评论区