Click here to Skip to main content
11,710,943 members (90,603 online)
Click here to Skip to main content

FindControl for Windows Forms

, 25 Nov 2009 CPOL 32.8K 10
Rate this:
Please Sign up or sign in to vote.
A quick implementation of FindControl for Windows Forms.

Some one asked a question in the Windows Mobile programming forums about dynamically finding a control at runtime. When I saw the question, the first solution that came to mind was Control.FindControl. Before I suggested using that method, I searched for the MSDN documentation and found that it only existed in ASP.NET. It's missing from Windows Forms all together. So I put together a quick implementation of the function.

Control FindControl(string target )
{
    return FindControl(this, target);
}

static Control FindControl(Control root, string target)
{
    if(root.Name.Equals(target))
        return root;
    for(var i=0;i<root.Controls.Count;++i)
    {
        if (root.Controls[i].Name.Equals(target))
            return root.Controls[i];
    }
    for(var i=0;i<root.Controls.Count;++i)
    {
        Control result;
       for(var k=0;k<root.Controls[i].Controls.Count;++k)
       {
           result = FindControl(root.Controls[i].Controls[k], target);
           if(result!=null)
               return result;
       }
    }
    return null;
}

To use the function, just define it on the root of the form in which you need the functionality. If you cannot define it on a root form, then you can use the static version of it, passing to the method a reference to a container of the control hierarchy in which you wish to search. The function will recursively search deeper and deeper into the control hierarchy until it finds a UI object with the name specified, or it will return null if no such object can be found.

License

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

Share

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 CodeProject.com 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.

Twitter:@J2iNet

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
aankitpal9-Jan-13 5:13
memberaankitpal9-Jan-13 5:13 
GeneralMy vote of 5 Pin
Member 96857978-Jan-13 7:15
memberMember 96857978-Jan-13 7:15 
GeneralMy vote of 1 Pin
lobotomy29-Dec-09 20:18
memberlobotomy29-Dec-09 20:18 
QuestionFind control by name Pin
RV Williams30-Nov-09 23:17
memberRV Williams30-Nov-09 23:17 
AnswerRe: Find control by name Pin
Joel Ivory Johnson30-Nov-09 23:21
memberJoel Ivory Johnson30-Nov-09 23:21 
GeneralWhy 3 loops? It can be much simpler with only 1 loop. Pin
Aviad P.26-Nov-09 10:30
memberAviad P.26-Nov-09 10:30 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Joel Ivory Johnson26-Nov-09 23:45
memberJoel Ivory Johnson26-Nov-09 23:45 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. [modified] Pin
Aviad P.27-Nov-09 0:50
memberAviad P.27-Nov-09 0:50 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Joel Ivory Johnson27-Nov-09 9:44
memberJoel Ivory Johnson27-Nov-09 9:44 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Aviad P.29-Nov-09 14:07
memberAviad P.29-Nov-09 14:07 
QuestionRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
stixoffire30-Nov-09 15:41
memberstixoffire30-Nov-09 15:41 
AnswerRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Joel Ivory Johnson30-Nov-09 16:10
memberJoel Ivory Johnson30-Nov-09 16:10 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
kLeZ-hAcK30-Nov-09 23:32
memberkLeZ-hAcK30-Nov-09 23:32 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Aviad P.1-Dec-09 4:09
memberAviad P.1-Dec-09 4:09 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
kLeZ-hAcK1-Dec-09 4:31
memberkLeZ-hAcK1-Dec-09 4:31 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Aviad P.1-Dec-09 8:17
memberAviad P.1-Dec-09 8:17 
GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. Pin
Aviad P.2-Dec-09 6:31
memberAviad P.2-Dec-09 6:31 

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.150819.1 | Last Updated 25 Nov 2009
Article Copyright 2009 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid