我现在把foreach写在一个自定义函数里面,函数体如下所述        public static void ForeachCtrl()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    .....
                }
            }
        }我想在调用这个函数的时候,TextBox这里可以用其他类型来替换,这样子的话,我在主程序地方只要定义我想要遍历的控件类型,这里就能查找出来不知道这样子说,各位高手能否明白,谢谢!!!

解决方案 »

  1.   

    public static void ForeachCtrl(System.Type t)
    {
    foreach (Control ctrl in this.Controls)
    {
    if (typeof(ctrl).FullName==t.FullName)
    {
    .....
    }
    }
    }
    这样应该可以吧
    传进控件的typeof(ctrl)
      

  2.   

    public static void ForeachCtrl( Type t)
            {
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl.GetType() == t )
                    {
                        .....
                    }
                }
            }
      

  3.   

    请问这样子的话,在主程序中,t怎么传过来???我直接写TextBox不行的!!!
      

  4.   


    or
    public static void ForeachCtrl( System.Web.UI.Control control )
            {
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl.GetType() == control.GetType() )
                    {
                        .....
                    }
                }
            }