winform中如何获得窗体中所有的控件

abc83915 2008-03-31 09:16:08
我想用遍例查出窗体内所有的botton控件。如何写?
...全文
921 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc83915 2008-03-31
  • 打赏
  • 举报
回复
我的button不在容器里。。但是跟踪了没执行这句话啊
h_w_king 2008-03-31
  • 打赏
  • 举报
回复
跟踪一下, 看 this.textBox1.Focus(); 有没有执行.

若你的button是在一个容器里, 则this.textBox1.Focus(); 就不会执行.

abc83915 2008-03-31
  • 打赏
  • 举报
回复
foreach (Control ctr in this.Controls)
{
if (ctr.getGetType().ToString() == "System.Windows.Forms.Button")
{
this.textBox1.Focus();
}
}

请问我这样写怎么不能让textBox1获得焦点呢?
starlessnt 2008-03-31
  • 打赏
  • 举报
回复
3楼的方法很好用的
h_w_king 2008-03-31
  • 打赏
  • 举报
回复 1
考虑容器控件. 若button在容器里.

private void seachbutton(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c is Button)
{ .....}
else
if (c.Controls.Count > 0)
seachbutton(c.Controls);
}

}


执行:
 seachbutton(this.Controls);
qfgyd2004 2008-03-31
  • 打赏
  • 举报
回复
 private List<Button> getButton()
{
foreach (Control c in this.Controls)
{
List<Button> list = new List<Button>();
if (c.GetType().ToString() == "System.Windows.Forms.Button")
{
list.Add(c);
}
}
return List;
}
ericzhangbo1982111 2008-03-31
  • 打赏
  • 举报
回复
BLL.dIsplayServerList(cboServers);
foreach (Control ctr in this.Controls)
{
if (ctr is Button)
{

}
}
HarleyTung 2008-03-31
  • 打赏
  • 举报
回复

foreach(Control ctl in Controls)
{
if(ctl is Button) //or if(ctl.GetType()==(new Button()).GetType)
..
//其它类似
}
HarleyTung 2008-03-31
  • 打赏
  • 举报
回复

foreach(Control c in this.Controls)
{
switch(c.GetType().Name.ToUpper())
{
case "Button":
default:
}
}
HarleyTung 2008-03-31
  • 打赏
  • 举报
回复

foreach(Control ctl in Controls)
{
...

}

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧