LINQ如何递归子控件

private void SetTextReadOnly(Control ctr, bool blReadOnly)
        {
            ctr.Controls.Cast<Control>().Select(s => s).ToList<Control>().ForEach(s =>
                {
                    if (s.GetType() == typeof(TextBox))
                        ((TextBox)s).ReadOnly = blReadOnly;

                    if (s.HasChildren)
                    {
                        SetTextReadOnly(s, blReadOnly);
                    }
                });

            //foreach (Control c in ctr.Controls)
            //{
            //    if (c.GetType() == typeof(TextBox))
            //    {
            //        ((TextBox)c).ReadOnly = true;
            //    }
            //    if (c.HasChildren)
            //    {
            //        SetTextReadOnly(c, blReadOnly);
            //    }
            //}
        }

  

posted @ 2014-08-07 16:13  跟着阿笨一起玩.NET  阅读(572)  评论(0编辑  收藏  举报