C# 按名称获取控件,包括子控件

C# 按名称获取控件,包括子控件,c#,.net,winforms,controls,C#,.net,Winforms,Controls,我正试着用名字来控制。我编写了以下代码: public Control GetControlByName(string name) { Control currentControl; for(int i = 0,count = Controls.Count; i < count; i++) { currentControl = Controls[i]; if (currentControl.HasChildren)

我正试着用名字来控制。我编写了以下代码:

public Control GetControlByName(string name)
{
    Control currentControl; 

    for(int i = 0,count = Controls.Count; i < count; i++)
    {
        currentControl = Controls[i];

        if (currentControl.HasChildren)
        {
            while (currentControl.HasChildren)
            {
                for(int x = 0,size = currentControl.Controls.Count; x < size; x++)
                {
                    currentControl = currentControl.Controls[x];

                    if (currentControl.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                    {
                        return currentControl;
                    }
                }
            }
        }
        else
        {
            if (currentControl.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                return currentControl;
            }
        }
    }

    return null;
}
公共控件GetControlByName(字符串名称)
{
控制电流控制;
for(int i=0,count=Controls.count;icurrentControl=currentControl.Controls[x];
if(currentControl.Name.Equals(Name,StringComparison.OrdinalIgnoreCase))
{
回流控制;
}
}
}
}
其他的
{
if(currentControl.Name.Equals(Name,StringComparison.OrdinalIgnoreCase))
{
回流控制;
}
}
}
返回null;
}

它只返回null。有人能指出我的错误吗?欢迎提供任何帮助或改进此代码的方法。

只需使用控件收集方法:


实际上,我在工作中编写了一些扩展方法来实现这一点:

public static class MyExensions ()
{
    public static Control FindControlRecursively (this Control control, string name)
    {
        Control result = null;

        if (control.ID.Equals (name))
        {
            result = control;
        }
        else
        {
            foreach (var child in control.Children)
            {
                result = child.FindControlRecursively (name);

                if (result != null)
                {
                    break;
                }
            }
        }

        return result;
    }

    public static T FindControlRecursively<T> (this Control control, string name)
        where T: Control
    {
        return control.FindControlRecursively (name) as T;
    }
}
编辑


当然,这是一种深度优先算法,根据控制链的深度,它可能会比较慢。如果您发现它太慢,您可以重写它以使用广度优先算法。

如果您使用
System.Windows.Forms
(这就是
.Find(string,bool)
的话,对不使用
的情况稍微调整一下

公共静态类MyExensions
{
以递归方式查找公共静态控件(此控件,字符串名称)
{
控制结果=空;
if(control.ID.Equals(name))
{
结果=对照;
}
其他的
{
对于(变量i=0;i

p、 是的,我知道这是一个老问题,但如果有帮助,currentControl=currentControl.Controls[x];循环看起来很可疑,为什么不使用递归?这是递归的吗?它会找到子对象的子对象吗?@User:是的,第二个要查找的参数是这样的。
public static class MyExensions ()
{
    public static Control FindControlRecursively (this Control control, string name)
    {
        Control result = null;

        if (control.ID.Equals (name))
        {
            result = control;
        }
        else
        {
            foreach (var child in control.Children)
            {
                result = child.FindControlRecursively (name);

                if (result != null)
                {
                    break;
                }
            }
        }

        return result;
    }

    public static T FindControlRecursively<T> (this Control control, string name)
        where T: Control
    {
        return control.FindControlRecursively (name) as T;
    }
}
public class MyForm : Form
{
    public void SetSomeText ()
    {
        var control = this.FindControlRecursively<TextBox> ("myTextboxName");

        if (control != null)
        {
            control.Text = "I found it!";
        }

        // Or...

        var control2 = this.FindControlRecursively ("myTextboxName2") as TextBox;

        if (control != null)
        {
            control2.Text = "I found this one, also!";
        }
    }
}
public static class MyExensions
{
    public static Control FindControlRecursively(this Control control, string name)
    {
        Control result = null;

        if (control.ID.Equals(name))
        {
            result = control;
        }
        else
        {
            for (var i = 0; i < control.Controls.Count; i++)
            {
                result = control.Controls[i].FindControlRecursively(name);

                if (result != null)
                {
                    break;
                }
            }
        }

        return result;
    }

    public static T FindControlRecursively<T>(this Control control, string name)
        where T : Control
    {
        return control.FindControlRecursively(name) as T;
    }
}