Click here to Skip to main content
15,890,377 members
Articles / Desktop Programming / Windows Forms

FindControl for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
25 Nov 2009CPOL 51.5K   10   17
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.

C#
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)


Written By
Software Developer
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. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
GeneralMy vote of 5 Pin
aankitpal9-Jan-13 5:13
aankitpal9-Jan-13 5:13 
GeneralMy vote of 5 Pin
Member 96857978-Jan-13 7:15
Member 96857978-Jan-13 7:15 
GeneralMy vote of 1 Pin
lobotomy29-Dec-09 20:18
professionallobotomy29-Dec-09 20:18 
QuestionFind control by name Pin
richardw4830-Nov-09 23:17
richardw4830-Nov-09 23:17 
AnswerRe: Find control by name Pin
Joel Ivory Johnson30-Nov-09 23:21
professionalJoel 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
Aviad 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
professionalJoel Ivory Johnson26-Nov-09 23:45 
The behaviour of the function with one loop is different than the behaviour when using three loops. When using one loop you implement a depth first search occurs. The one I implemented performs a breadth first search. The results from the two different implementations can be entirely different if you happen to have nested controls that have the same name as a higher level control.

Let's say you have the following control hierarchy
Form1
  |
  +-UserControl1
  |  |
  |  +-TextBox1
  |  |
  |  +-TextBox2
  |
  +-TextBox1



If you were to call FindControl for the above using the depth first loop that you've defined if you try to get TextBox1 then the textbox inside of the user control would be returned. If you were to call it with the implementation that I listed then the TextBox1 that is the direct child of Form1 would be returned. The decision to go with a breadth first search is so that the element with the same name that is most local to the root element is returned. Though from looking at your code I do see a way that mine could be simplified.

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;
        result=FindControl(root.Controls[i],target);
        if(result!=null)
            return result;
    }
    return null;
}



GeneralRe: Why 3 loops? It can be much simpler with only 1 loop. [modified] Pin
Aviad P.27-Nov-09 0:50
Aviad 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
professionalJoel 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
Aviad 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
stixoffire30-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
professionalJoel 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
kLeZ-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
Aviad 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
kLeZ-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
Aviad 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
Aviad P.2-Dec-09 6:31 

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.