Introduction
Have you ever wanted to easily gain access to every control on a form? This code, though simple, is a way to do just that. In the code provided below, you will see that I used a binary tree traversal method and the Tag
property of the controls to change the index of every ComboBox
to zero. You can easily modify this code to do whatever you want to each control type.
Dim branchNode As Control = Me
Dim branchLevel As Integer = 0
Dim numBranchesOnLevel As Integer = Me.Controls.Count
While numBranchesOnLevel > 0
branchNode.Tag = 0
branchNode = branchNode.Controls(branchNode.Tag)
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
End While
If branchNode.GetType.ToString = "System.Windows.Forms.ComboBox" Then
Dim theCombo As ComboBox = branchNode
theCombo.SelectedIndex = 0
End If
For branchLevel = branchLevel To 1 Step -1
branchNode = branchNode.Parent
numBranchesOnLevel = branchNode.Controls.Count
branchNode.Tag += 1
If Not branchNode.Tag >= numBranchesOnLevel Then
While numBranchesOnLevel > 0
branchNode = branchNode.Controls(branchNode.Tag)
branchNode.Tag = 0
numBranchesOnLevel = branchNode.Controls.Count
branchLevel += 1
End While
End If
If branchNode.GetType.ToString = "System.Windows.Forms.ComboBox" Then
Dim theCombo As ComboBox = branchNode
theCombo.SelectedIndex = 0
End If
Next branchLevel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.