kejames 學習筆記本

這裡是Kejames的筆記本,歡迎各位網友給予指教,謝謝。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WinForm利用Recursive找出Controls

Posted on 2007-09-05 19:23  Kejames  阅读(144)  评论(0)    收藏  举报
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);
                }

            }

        }



    }

    
}