C# 获取控件里的子控件的名称

利用Control类的Controls可取得子控件集合,依据Controls.Count获得的子控件数目,用Control.Controls[n].属性名 访问相关属性

 

  private void ResetTextBox(System.Windows.Forms.Control.ControlCollection cc, Boolean boolean)
        {

            // 利用Control类的Controls可取得子控件集合

            foreach (Control ctr in cc)
            {

                if (ctr is TextBox)
                {

                    string ctrName = ctr.Name;
                    ctrName = ctrName.Substring(ctrName.Length - 1);
                   
                    int i = int.Parse(ctrName);

                    if (i > 0 && i <= 9) { ((TextBox)ctr).ReadOnly = boolean; }


                }
            }
        }

 


调用时:ResetTextBox(this.Controls);

posted @ 2012-04-21 11:56  小四儿  阅读(1379)  评论(1编辑  收藏  举报