c# WinForm中查找控件,动态赋值

来源:互联网 发布:淘宝直播的东西能买吗 编辑:程序博客网 时间:2024/05/16 18:54
        //<summary>        /// 在winform中查找控件        ///</summary>        ///<param ></param>        ///<param ></param>        ///<returns></returns>        private System.Windows.Forms.Control findControl(System.Windows.Forms.Control control, string controlName)        {            Control c1;            foreach (Control c in control.Controls)            {                if (c.Name == controlName)                {                    return c;                }                else if (c.Controls.Count >0)                {                    c1 = findControl(c, controlName);                    if (c1 !=null)                    {                        return c1;                    }                }            }            return null;        }



具体调用:


 private void button1_Click(object sender, EventArgs e)        {            //调用            for (int i = 1; i <= 30; i++)            {                string _box = "b" + i.ToString();                if (this.findControl(groupBox1, _box) != null)                {                    TextBox tb = (TextBox)this.findControl(groupBox1, _box);                    tb.Text = i.ToString();                }            }         }