Click here to Skip to main content
Click here to Skip to main content

Non-recursive method to set every control on a form

, 1 Nov 2006
Rate this:
Please Sign up or sign in to vote.
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.

'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)

Share

About the Author

Ky Sealy
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralTHE NEW IMPLEMENTATION PinmemberK. Sealy10-Nov-06 8:06 
GeneralI made it even shorter! PinmemberPinx10-Jan-07 2:52 
GeneralActually the Idea is pretty good PinmemberTimothy Paul Narron9-Nov-06 10:01 
AnswerRe: Actually the Idea is pretty good PinmemberK. Sealy10-Nov-06 4:44 
AnswerRe: Actually the Idea is pretty good PinmemberTimothy Paul Narron10-Nov-06 5:48 
GeneralOr.. you could just traverse the controls collection.. PinmemberPat Tormey7-Nov-06 0:46 
AnswerRe: Or.. you could just traverse the controls collection.. [modified] PinmemberK. Sealy7-Nov-06 2:43 
QuestionWhy not recurse? PinmemberJohn Cardinal6-Nov-06 9:14 
AnswerRe: Why not recurse? PinmembereisernWolf6-Nov-06 19:43 
AnswerRe: Why not recurse? PinmemberK. Sealy7-Nov-06 2:46 
AnswerRe: Why not recurse? Pinmemberchaldon6-Nov-06 22:03 
AnswerRe: Why not recurse? [modified] PinmemberK. Sealy7-Nov-06 2:49 
GeneralRe: Why not recurse? Pinmemberchaldon7-Nov-06 3:41 
AnswerRe: Why not recurse? PinmemberK. Sealy7-Nov-06 3:55 
AnswerRe: Why not recurse? PinmemberK. Sealy7-Nov-06 3:09 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 1 Nov 2006
Article Copyright 2006 by Ky Sealy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid