>>>>>>>>>>如何遍历在form中的所有子控件<<<<<<<<<<

isage2002 2003-08-26 06:13:13
form中包含TabControl1,groupbox等控件容器
我想遍历from上的,以及form上各种控件容器内的子控件
...全文
34 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
isage2002 2003-08-26
  • 打赏
  • 举报
回复
Private Sub dataList()
getChildCon(Me)
End Sub

Private Sub getChildCon(ByVal c As Control)
If c.HasChildren Then
For Each MyObject As Control In c.Controls
getChildCon(MyObject)
Next
Else
MsgBox(c.Name)
End If
End Sub
isage2002 2003-08-26
  • 打赏
  • 举报
回复
不是很通用,假如form上有TabControl1,TabControl1上又有groupbox,就很麻烦
能不能像vb6那样不需要递归遍历仅一次遍历就可以呢?

panyee 2003-08-26
  • 打赏
  • 举报
回复
private void button2_Click(object sender, System.EventArgs e)
{
foreach(Control c in this.Controls)
{
MessageBox.Show(c.Name);
GetControls(c);
}
}

private void GetControls(Control ctl)
{
if(ctl.HasChildren)
{
foreach(Control c in ctl.Controls)
{
MessageBox.Show(c.Name);
GetControls(c);
}
}
}
isage2002 2003-08-26
  • 打赏
  • 举报
回复
to :panyee(快乐王子) ( )
那么简单吗?不会吧
放两个TabControl1,groupbox
再在里面放一些textbox 看看你找到了什么?
嘿嘿
isage2002 2003-08-26
  • 打赏
  • 举报
回复
我是这么写的
For Each MyObject As Object In Me.Controls

If (MyObject.GetType Is (New TextBox).GetType) Then
MsgBox(CType(MyObject, TextBox).Name)
End If

Next
但是仅仅找到了控件容器TabControl1,groupbox
我想找到from上所有的控件,怎么办?
wingchi 2003-08-26
  • 打赏
  • 举报
回复
up
wingchi 2003-08-26
  • 打赏
  • 举报
回复
i[
panyee 2003-08-26
  • 打赏
  • 举报
回复
foreach(Control c in this.Controls)
{
MessageBox.Show(c.Name);
}

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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