Best way to loop thru controls on a form recursively?

What is the most efficient way to loop thru all the controls on a form including controls within container controls?

Thanks!
BlakeMcKennaAsked:
Who is Participating?
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
	Public Sub LoopThroughControls(container As Control)

		For Each ctl As Control In container.Controls
			'Do your stuff here...
			If ctl.Controls.Count > 0 Then
				LoopThroughControls(ctl)
			End If
			'... or here
		Next

	End Sub

Open in new window

A form is a Control, so you can send your form to that method, and it will recursively calls itself back when you encounter a Control that contains other Controls.
0
 
BlakeMcKennaAuthor Commented:
Thanks James...exactly what I was looking for!
0
All Courses

From novice to tech pro — start learning today.