计算机二级

3773考试网计算机等级考试计算机二级正文

如何用C#在winform中查找控件

来源:fjzsksw.com 2010-8-10 14:44: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;

  }

  //调用

  for (int i = 1; i <= 30; i++)

  {

  string _box = "b" + i.ToString();

  TextBox tb = (TextBox)this.findControl(groupBox2, _box);

  tb.Text = i.ToString();

  }

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1