Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / Visual Basic

Non-recursive method to set every control on a form

Rate me:
Please Sign up or sign in to vote.
1.83/5 (8 votes)
1 Nov 2006CPOL 38.8K   15   15
This article discusses a non-recursive method to gain access to every control on a form.

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.

VB
'Traverse all controls on a form and do something at every node
'(ie. check for ComboBox and set index = 0)
'The tag property of the controls is used to track which nodes have been traversed.

'******************************************************
Dim branchNode As Control = Me      'Current Control
Dim branchLevel As Integer = 0      'Level of branch from top to bottom (ie. most top = 0)
Dim numBranchesOnLevel As Integer = Me.Controls.Count   'Number of branches on current level

While numBranchesOnLevel > 0    'Start at lowest left branch
    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
'Do this at first node
    Dim theCombo As ComboBox = branchNode
    theCombo.SelectedIndex = 0
End If

For branchLevel = branchLevel To 1 Step -1  'Traverse tree up
    branchNode = branchNode.Parent
    numBranchesOnLevel = branchNode.Controls.Count
    branchNode.Tag += 1
    If Not branchNode.Tag >= numBranchesOnLevel Then
        While numBranchesOnLevel > 0    'Traverse tree right
            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
    'Do this at every node
        Dim theCombo As ComboBox = branchNode
        theCombo.SelectedIndex = 0
    End If
Next branchLevel
'******************************************************

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralTHE NEW IMPLEMENTATION Pin
K. Sealy10-Nov-06 8:06
K. Sealy10-Nov-06 8:06 
GeneralI made it even shorter! Pin
pinx10-Jan-07 2:52
pinx10-Jan-07 2:52 
GeneralActually the Idea is pretty good Pin
Timothy Paul Narron9-Nov-06 10:01
Timothy Paul Narron9-Nov-06 10:01 
AnswerRe: Actually the Idea is pretty good Pin
K. Sealy10-Nov-06 4:44
K. Sealy10-Nov-06 4:44 
AnswerRe: Actually the Idea is pretty good Pin
Timothy Paul Narron10-Nov-06 5:48
Timothy Paul Narron10-Nov-06 5:48 
GeneralOr.. you could just traverse the controls collection.. Pin
Pat Tormey7-Nov-06 0:46
Pat Tormey7-Nov-06 0:46 
AnswerRe: Or.. you could just traverse the controls collection.. [modified] Pin
K. Sealy7-Nov-06 2:43
K. Sealy7-Nov-06 2:43 
QuestionWhy not recurse? Pin
Member 966-Nov-06 9:14
Member 966-Nov-06 9:14 
AnswerRe: Why not recurse? Pin
eisernWolf6-Nov-06 19:43
eisernWolf6-Nov-06 19:43 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 2:46
K. Sealy7-Nov-06 2:46 
AnswerRe: Why not recurse? Pin
chaldon6-Nov-06 22:03
chaldon6-Nov-06 22:03 
AnswerRe: Why not recurse? [modified] Pin
K. Sealy7-Nov-06 2:49
K. Sealy7-Nov-06 2:49 
GeneralRe: Why not recurse? Pin
chaldon7-Nov-06 3:41
chaldon7-Nov-06 3:41 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 3:55
K. Sealy7-Nov-06 3:55 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 3:09
K. Sealy7-Nov-06 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.