Click here to Skip to main content
12,695,076 members (31,249 online)
Click here to Skip to main content
Add your own
alternative version

Stats

30.9K views
15 bookmarked
Posted

Non-recursive method to set every control on a form

, 1 Nov 2006 CPOL
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

You may also be interested in...

Comments and Discussions

 
GeneralTHE NEW IMPLEMENTATION Pin
K. Sealy10-Nov-06 9:06
memberK. Sealy10-Nov-06 9:06 
GeneralI made it even shorter! Pin
Pinx10-Jan-07 3:52
memberPinx10-Jan-07 3:52 
GeneralActually the Idea is pretty good Pin
Timothy Paul Narron9-Nov-06 11:01
memberTimothy Paul Narron9-Nov-06 11:01 
AnswerRe: Actually the Idea is pretty good Pin
K. Sealy10-Nov-06 5:44
memberK. Sealy10-Nov-06 5:44 
AnswerRe: Actually the Idea is pretty good Pin
Timothy Paul Narron10-Nov-06 6:48
memberTimothy Paul Narron10-Nov-06 6:48 
GeneralOr.. you could just traverse the controls collection.. Pin
Pat Tormey7-Nov-06 1:46
memberPat Tormey7-Nov-06 1:46 
AnswerRe: Or.. you could just traverse the controls collection.. [modified] Pin
K. Sealy7-Nov-06 3:43
memberK. Sealy7-Nov-06 3:43 
QuestionWhy not recurse? Pin
John Cardinal6-Nov-06 10:14
memberJohn Cardinal6-Nov-06 10:14 
AnswerRe: Why not recurse? Pin
eisernWolf6-Nov-06 20:43
membereisernWolf6-Nov-06 20:43 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 3:46
memberK. Sealy7-Nov-06 3:46 
AnswerRe: Why not recurse? Pin
chaldon6-Nov-06 23:03
memberchaldon6-Nov-06 23:03 
AnswerRe: Why not recurse? [modified] Pin
K. Sealy7-Nov-06 3:49
memberK. Sealy7-Nov-06 3:49 
GeneralRe: Why not recurse? Pin
chaldon7-Nov-06 4:41
memberchaldon7-Nov-06 4:41 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 4:55
memberK. Sealy7-Nov-06 4:55 
AnswerRe: Why not recurse? Pin
K. Sealy7-Nov-06 4:09
memberK. Sealy7-Nov-06 4: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 1 Nov 2006
Article Copyright 2006 by Ky Sealy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid