Reputation: 395
I want to get a list of all controls; I managed to do that in the code below; the issue is that this does list the controls outside containers only. I want to get list of all controls including those inside containers (such as controls inside TabControl). My Question is: How to do this in VB.Net?
Sub ListAllControls()
Me.RichTextBox1.Clear()
Dim MekdamCTL As Control
For Each MekdamCTL In Me.Controls
Me.RichTextBox1.AppendText("Control: " & MekdamCTL.Name & Environment.NewLine)
Next
Me.RichTextBox1.AppendText("How to include ALL Controls in ALL Containers, such as Controls in TabControl ??")
End Sub
Thanks in advance for help and/or comments.
Upvotes: 0
Views: 2379
Reputation: 1908
May This Can Be Used:
Private Sub frmWRMenu_Activated(sender As Object, e As EventArgs) Handles Me.Activated
Dim k As New List(Of String)
Dim myNumber = 0
Dim myCtrl As Control = Me
Repeat:
For a = 1 To myCtrl.Controls.Count
k.Add(myCtrl.Controls(a - 1).Name)
Debug.Print(myCtrl.Controls(a - 1).Name)
Next
If (myNumber + 1) <= k.Count Then
myCtrl = Me.Controls.Find(k(myNumber ), True)(0)
myNumber += 1
GoTo Repeat
End If
End Sub
Upvotes: 0
Reputation: 11773
Give this a try:
'this loop will get all the controls on the form
'no matter what the level of container nesting
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
RichTextBox1.AppendText("Control: " & ctrl.Name & Environment.NewLine)
ctrl = Me.GetNextControl(ctrl, True)
Loop
With this method there is no need for checking or recursion.
Upvotes: 1
Reputation: 216353
You need a recursive function that you can call from your main form
Public Sub ListControls(coll As Control.ControlCollection)
for each ctr in coll
Me.RichTextBox1.AppendText("Control: " & ctr.Name & Environment.NewLine)
if ctr.Controls.Count > 0 Then
ListControls(ctr.Controls)
End if
Next
End Sub
Call it from your main form
ListControls(Me.Controls)
Upvotes: 2
Reputation: 4534
This is a little more complex than you need, but the following function returns all the controls of any given type and optionally searches any embedded containers
''' <summary>Get all Controls of a specified type within a container and optionally any embedded containers</summary>
''' <typeparam name="ctrlType">The type of Control to be searched for (must inherit from Control)</typeparam>
''' <param name="parent">The top level container</param>
''' <param name="searchContainers">True if embedded containers are to be searched</param>
''' <returns>IEnumerable with all Controls of the specified type that were found</returns>
Function GetControlsOfType(Of ctrlType As Control)(parent as Control, searchContainers As Boolean) As IEnumerable(Of ctrlType)
Dim ctrls As New List(Of ctrlType)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is ctrlType Then ctrls.Add(DirectCast(ctrl, ctrlType))
If searchContainers AndAlso ctrl.Controls.Count > 0 Then ctrls.AddRange(GetControlsOfType(Of ctrlType)(ctrl, searchContainers))
Next
Return ctrls
End Function
If you want to list all controls of any type, you can call it like this:
For Each ctrl As Control In GetControlsOfType(Of Control)(Me, True)
Me.RichTextBox1.AppendText("Control: " & ctrl.Name & vbCrLf)
Next
Upvotes: 0