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

4

我想通过名称获取一个控件。我编写了以下代码:

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;
}

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

循环中的 currentControl = currentControl.Controls[x]; 看起来有问题。 - kenny
http://stackoverflow.com/questions/653284/get-available-controls-from-form-using-c-sharp - keyboardP
2
你为什么不使用递归? - M.Babcock
3个回答

13

只需使用控件集合的Find方法:

            var aoControls = this.Controls.Find("MyControlName", true);
            if ((aoControls != null) && (aoControls.Length != 0))
            {
                Control foundControl = aoControls[0];
            }

这是递归的吗?它会找到子孙吗? - User
@用户:是的,Find函数的第二个参数可以实现这个功能。 - competent_tech

1
如果您没有使用System.Windows.Forms(这也是.Find(string, bool)的工作方式),则需要进行一些微调。
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;
    }
}

p.s. 是的,我知道这是一个老问题,但如果它有帮助的话


1

我在工作中编写了一些扩展方法来完成这件事:

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;
    }
}

注意:为了简单起见,已删除空值检查。

您可以使用以下方式在表单上查找TextBox

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!";
        }
    }
}

编辑

当然,这是一种深度优先算法,取决于您的控制链有多深,可能会很慢。如果您发现它太慢,您可以重写它以使用广度优先算法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接