Click here to Skip to main content
13,453,774 members (44,089 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 30 Nov 2009

Improved FindControl for Windows desktop and mobile

, 30 Nov 2009
Rate this:
Please Sign up or sign in to vote.
An improved FindControl for Windows desktop and mobile.

A few days ago I posted some code containing an implementation for a Windows Forms version of FindControl. Aviad P. pointed out a correction and a way that the routine can be simlified. While I had intended the original code to do a breadth-first search, it was doing a combination of depth and breadth. The functionality could also be implemented with a single loop. The result of the messages, the resulting code from our communication, is shown below:

Control FindControl(string target) {
    return FindControl(this,target);
static Control FindControl(Control root, string target){
    if(root.Name == target)
        return root;
    List currentLevel = new List() { root };
    while (currentLevel.Count > 0)
        Control match = currentLevel.FirstOrDefault(x => x.Name == target);
        if (match != null) return match;
        currentLevel = currentLevel.SelectMany(x => x.Controls.Cast()).ToList();
    }    return null;


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


About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.

For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.

While most of my articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.


You may also be interested in...


Comments and Discussions

GeneralYour generic argument specifications are lost in the copy/paste Pin
Aviad P.4-Dec-09 2:34
memberAviad P.4-Dec-09 2:34 
GeneralRe: Your generic argument specifications are lost in the copy/paste Pin
Joel Ivory Johnson4-Dec-09 2:40
memberJoel Ivory Johnson4-Dec-09 2:40 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180318.3 | Last Updated 30 Nov 2009
Article Copyright 2009 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid