Hi,
When iterating through the controls on a form, it seems that controls contained within another control are ignored. Is that correct?
Also, it seems impossible to iterate through a container control's controls.
Any ideas please?
Printable View
Hi,
When iterating through the controls on a form, it seems that controls contained within another control are ignored. Is that correct?
Also, it seems impossible to iterate through a container control's controls.
Any ideas please?
Hi.
You could use a recursive loop.
VB Code:
Public Sub LoopControls(Parent as Control) Dim cc As ControlCollection Dim c As Control If Parent is Nothing Then cc=Me.Controls Else cc=Parent.Controls End If For Each c In cc 'Do whatever it is you wanna do. 'Call the sub again, this time using the current control as parent. If c.Controls.Count>0 Then LoopControls(c) Next End Sub
Just call it like LoopControls(Nothing) to start at the form itself.
Hope this helps you.
Taxes,
You are correct, controls contained in another control are not listed as part of the parent's controls, however it is possible to iterate through a container controls collection of contained controls.
eg.
VB Code:
Dim ctl1 As Control, ctl2 As Control For Each ctl1 In Me.Controls If ctl1.Name = "Panel1" Then For Each ctl2 In ctl1.Controls If ctl2.Name = "Button1" Then MessageBox.Show("Found It!") Next End If Next
Hi CyberHawke
Many thanks, it works O.K.
Hi Pax,
I'll try your suggestion later, Thanks.
Another option you could use when knowing which container control you are accessing is:
VB Code:
Dim ctl As Control For Each ctl In Me.Panel1.Controls If ctl.Name = "Button1" Then MessageBox.Show("Found It!") Next
Even better:wave:Quote:
Originally posted by CyberHawke
Another option you could use when knowing which container control you are accessing is:
VB Code:
Dim ctl As Control For Each ctl In Me.Panel1.Controls If ctl.Name = "Button1" Then MessageBox.Show("Found It!") Next