Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Improved FindControl for Windows desktop and mobile

, 30 Nov 2009 CPOL
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.
Follow on   Twitter   LinkedIn

Comments and Discussions

GeneralYour generic argument specifications are lost in the copy/paste PinmemberAviad P.4-Dec-09 3:34 
GeneralRe: Your generic argument specifications are lost in the copy/paste PinmemberJoel Ivory Johnson4-Dec-09 3:40 

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 | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 30 Nov 2009
Article Copyright 2009 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid