碧水寒潭

追寻平淡的幸福:和喜欢的人在一起,做自己喜欢的事……
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 在winform中查找控件

Posted on 2010-08-09 22:04  碧水寒潭  阅读(5303)  评论(0编辑  收藏  举报

 

代码
/// <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;
}

//调用

for (int i = 1; i <= 30; i++)
{
string _box = "b" + i.ToString();
TextBox tb
= (TextBox)this.findControl(groupBox2, _box);
tb.Text
= i.ToString();
}