Shandy's Blog

Where Andrew Sutton, aka Shandy rants and rambles on as the fancy takes him

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

My name is Andrew Sutton, aka Shandy.

I am currently living and working in the UK as a software developer. This blog contains mainly IT related issues.

I was a Microsoft VB MVP for a couple of years (Apr 2004-Mar 2006) and was a vbCityLeader between April 2003 and June 2007.

If you are looking for my Sri Lanka or Morocco experiences check out Shandy's Sri Lanka Blog or Shandy's Morocco Blog. My personal (Non IT) blog is now at Shandy's Place

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Articles

Archives

Topics

Image Galleries

.NET

Blogs I Read

Others

VB.Classic

How To Enumerate Through All Controls On A Form

By Andrew Sutton, Microsoft VB MVP & vbCity Leader
Article Information

Language:

IDE:

Framework:

Area:

Audience:

VB.NET

Any

1.x

Windows.Forms (Controls)

Beginner, Intermediate, Expert

The Standard Method Of Enumerating Controls On A Form

When you enumerate the controls on a form only the controls whose parent is the form are enumerated. By this I mean that all controls placed directly onto the form will be enumerated but any controls placed into container controls like group boxes will not be enumerated. The code shows the method of enumerating which returns only controls placed directly onto the form.

Standard Enumeration Code
Dim oControl as Control
Dim oCollection As New Collection
For Each oControl In Me.Controls
  oCollection.Add(oControl)
Next

An Enhanced Method Of Enumerating All Controls On A Form

I found the lack of a direct method to enumerate all controls on a form a real nuisance. However, help is at hand. I have created a couple of functions that will enumerate all controls on a form.

Enhanced Enumeration Code
Private Function EnumerateAllControlsOnAForm(ByVal oForm As Form) As Collection
  Dim oControl As Control
  Dim oCollection As New Collection
  For Each oControl In oForm.Controls
    oCollection.Add(oControl)
    If oControl.Controls.Count > 0 Then
    '  Control is container control to enumerate through this control
      EnumerateAllControlsInAContainerControl(oControl, oCollection)
    End If
  Next
  Return oCollection
End Function
Private Sub EnumerateAllControlsInAContainerControl(ByVal oContainer As Control _
                                                   , ByRef oCollection As Collection _
                                                   )
  Dim oControl As Control
  For Each oControl In oContainer.Controls
    oCollection.Add(oControl)
    If oControl.Controls.Count > 0 Then
    '  Control is container control to enumerate through this control
      EnumerateAllControlsInAContainerControl(oControl, oCollection)
    End If
  Next
End Sub

All you need to do is pass a form to the EnumerateAllControlsOnAForm function it will return a Collection of all the controls on a form including all those in container controls.


Additional Reading

I solved this problem with help from Robert Verpalen who has published a slightly different way of doing it in C#, Enumerating all controls on a form.

posted on Wednesday, April 13, 2005 6:09 PM