GetAllControls – Retrieving all the controls inside a container and its sub-containers

' Returns an array with all the controls in the specified container control and ' its child containers' Example:'    ' print the name of all the controls on the form and its child container ' controls'    Dim ctl As Control'    For Each ctl In GetAllControls(Me)'        Debug.WriteLine(ctl.Name)'    NextFunction GetAllControls(ByVal container As Control) As Control()    Dim al As New ArrayList    Dim ctl As Control    For Each ctl In container.Controls        GetAllControlsHelper(ctl, al)    Next    Return al.ToArray(GetType(Control))End FunctionSub GetAllControlsHelper(ByVal container As Control, ByVal al As ArrayList)    ' add this control to the ArrayList    al.Add(container)    ' add all its child controls, by calling this routine recursively    Dim ctl As Control    For Each ctl In container.Controls        GetAllControlsHelper(ctl, al)    NextEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: