如何遍历winform窗体中的所有控件

nxd1985 2010-09-20 02:35:04
如何遍历winform窗体中的所有控件,包括容器中的
...全文
2528 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanwenzhi00 2011-04-08
  • 打赏
  • 举报
回复
递归可以的
kj289907795 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yunfei5555 的回复:]
foreach (Control ctl in this.Controls)
[/Quote]
+1
捷哥1999 2010-09-21
  • 打赏
  • 举报
回复
        private void GetControl(Control.ControlCollection ctc)
{
foreach (Control ct in ctc)
{
//C# 只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.HasChildren)
{
GetControl(ct.Controls);
}
}
}
lh1611 2010-09-21
  • 打赏
  • 举报
回复
学习了。
wuyq11 2010-09-20
  • 打赏
  • 举报
回复
void Foo(Control.ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
if (ctl is TextBox)
{
}
else if (ctl.Controls != null) Foo(ctl.Controls);
}
}
assky124 2010-09-20
  • 打赏
  • 举报
回复
一个简单的递归

//伪代码
void PrintCtrlName(Control parent)
{
foreach(Contrl ctrl in parent.Controls)
{
Print(ctrl.Name);
if(ctrl.Controls.Count >0)
{
PrintCtrlName(ctrl);
}
}
}

ZXY900213 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 monkchen 的回复:]

要用递归啊,楼上都是来混分的

//伪代码如下

void EnumControls(Control container)
{
foreach(var c in container.Controls)
{
//c is the child control here
EnumControls(c);
}
}

……
[/Quote]
+1
wangxianshou 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 monkchen 的回复:]
要用递归啊,楼上都是来混分的

//伪代码如下

void EnumControls(Control container)
{
foreach(var c in container.Controls)
{
//c is the child control here
EnumControls(c);
}
}

//调用
EnumControls(……
[/Quote]

很对,要用递归才可以
wangxianshou 2010-09-20
  • 打赏
  • 举报
回复
楼主,这是我写的一个类似的小函数,指定控件的
楼主可以自己改造下就可以了

可以弄一个一维数组,找到一个就放入,最后返回这个
数组即可。

http://blog.csdn.net/wangxianshou/archive/2010/08/24/5835145.aspx
monkchen 2010-09-20
  • 打赏
  • 举报
回复
要用递归啊,楼上都是来混分的

//伪代码如下

void EnumControls(Control container)
{
foreach(var c in container.Controls)
{
//c is the child control here
EnumControls(c);
}
}

//调用
EnumControls(this);
mingz1113 2010-09-20
  • 打赏
  • 举报
回复
可以查到的,用容器ID
foreach(Control c in groupBox1.Controls)
{
MessageBox.show(c.Name);//输出控件名
}
hanzhehanzhe 2010-09-20
  • 打赏
  • 举报
回复
5楼正解
ZengHD 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nxd1985 的回复:]

引用 1 楼 yunfei5555 的回复:
foreach (Control ctl in this.Controls)


假如窗体中有几个groupbox 这样取到的是容器
容器内部的怎么去做
[/Quote]

菜单、子菜单查不出来
看不见的控件查不出来
freeboy827 2010-09-20
  • 打赏
  • 举报
回复
继续遍历



nxd1985 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yunfei5555 的回复:]
foreach (Control ctl in this.Controls)
[/Quote]

假如窗体中有几个groupbox 这样取到的是容器
容器内部的怎么去做
qqrto 2010-09-20
  • 打赏
  • 举报
回复
支持1,2楼
铛铛 2010-09-20
  • 打赏
  • 举报
回复
foreach(Control ctrl in this.Controls)
{
...
}
yunfei5555 2010-09-20
  • 打赏
  • 举报
回复
foreach (Control ctl in this.Controls)

111,129

社区成员

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

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

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