using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender , EventArgs e)
{
ArrayList aryLst = FindControls(typeof(TextBox));
MessageBox.Show(aryLst.Count.ToString());
}
public ArrayList FindControls(Type type)
{
ArrayList list = new ArrayList();
foreach(Control c in this.Controls)
{
if(c.GetType() == type)
list.Add(c);
else
FindControlsRecursive(c, type, ref list);
}
return list;
}
private static void FindControlsRecursive(Control root, Type type, ref ArrayList list)
{
if(root.Controls.Count != 0)
{
foreach(Control c in root.Controls)
{
if(c.GetType() == type)
list.Add(c);
else if (c.HasChildren)
FindControlsRecursive(c, type, ref list);
}
}
}

}
}


浙公网安备 33010602011771号