|
-
Jul 6th, 2004, 06:38 AM
#1
Thread Starter
PowerPoster
Iterating through contained controls. (Resolved)
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?
Last edited by taxes; Jul 6th, 2004 at 09:16 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 6th, 2004, 06:58 AM
#2
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.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 6th, 2004, 07:03 AM
#3
Hyperactive Member
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
Last edited by CyberHawke; Jul 6th, 2004 at 07:09 AM.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 07:46 AM
#4
Thread Starter
PowerPoster
Hi CyberHawke
Many thanks, it works O.K.
Hi Pax,
I'll try your suggestion later, Thanks.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 6th, 2004, 07:49 AM
#5
Hyperactive Member
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
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 6th, 2004, 07:57 AM
#6
Thread Starter
PowerPoster
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
Even better
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|