Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在TabControl中查找控件_C#_.net - Fatal编程技术网

C# 在TabControl中查找控件

C# 在TabControl中查找控件,c#,.net,C#,.net,全部, 我试图在TabControl中按名称查找控件。但是,我当前的方法不适用于控件的子级。最好的方法是什么 Control control = m_TabControlBasicItems.Controls[controlName]; 控件始终为空,因为它低于两(或三)个级别。TabPage、GroupBox,对于单选按钮,有时还包括Panel 谢谢大家! 尝试在选项卡面板中的所有容器上循环: foreach(var c in tab_panel.Controls) { if(c is

全部,

我试图在TabControl中按名称查找控件。但是,我当前的方法不适用于控件的子级。最好的方法是什么

Control control = m_TabControlBasicItems.Controls[controlName];
控件始终为空,因为它低于两(或三)个级别。TabPage、GroupBox,对于单选按钮,有时还包括Panel


谢谢大家!

尝试在选项卡面板中的所有容器上循环:

foreach(var c in tab_panel.Controls)
{
   if(c is your control)
     return c;

   if(c is a container)
     loop through all controls in c;//recursion
}

.NET不公开搜索嵌套控件的方法。您必须自己实现递归搜索

下面是一个例子:

public class MyUtility
    {
        public static Control FindControl(string id, ControlCollection col)
        {
            foreach (Control c in col)
            {
                Control child = FindControlRecursive(c, id);
                if (child != null)
                    return child;
            }
            return null;
        }

        private static Control FindControlRecursive(Control root, string id)
        {
            if (root.ID != null && root.ID == id)
                return root;

            foreach (Control c in root.Controls)
            {
                Control rc = FindControlRecursive(c, id);
                if (rc != null)
                    return rc;
            }
            return null;
        }
    }

您需要递归所有控件以找到正确的控件

这对你来说是一个完美的例子。您应该能够复制和粘贴,并称之为w/o mods

粘贴代码段以防链接死掉

/// <summary>
/// Finds a Control recursively. Note finds the first match and exists
/// </summary>
/// <param name="container">The container to search for the control passed. Remember
/// all controls (Panel, GroupBox, Form, etc are all containsers for controls
/// </param>
/// <param name="name">Name of the control to look for</param>
public Control FindControlRecursive(Control container, string name)
{
    if (container == name) return container;

    foreach (Control ctrl in container.Controls)
    {
        Control foundCtrl = FindControlRecursive(ctrl, name);

        if (foundCtrl != null) return foundCtrl;
    }

    return null;
}
//
///递归地查找控件。注意:找到第一个匹配项并存在
/// 
///用于搜索已传递控件的容器。记得
///所有控件(面板、组框、窗体等)都是控件的容器
/// 
///要查找的控件的名称
公共控件FindControlRecursive(控件容器,字符串名称)
{
如果(container==name)返回container;
foreach(container.Controls中的控件ctrl)
{
Control foundCtrl=FindControl递归(ctrl,名称);
如果(foundCtrl!=null)返回foundCtrl;
}
返回null;
}

< />代码>

MMM,您考虑了接口而不是名称吗?将控件的类型更改为从控件的当前类型派生的类,并实现标识所需控件的接口。然后,可以递归地循环使用选项卡的子控件来查找实现接口的控件。可以在不破坏代码的情况下更改控件的名称,并进行编译时检查(名称中没有键入错误)。

尝试以下操作:

Control FindControl(Control root, string controlName)
{
    foreach (Control c in root.Controls)
    {
        if (c.Controls.Count > 0)
            return FindControl(c);
        else if (c.Name == controlName)
            return c;            
    }
    return null;
}
请参阅MSDN以了解

System.Web.UI.Control.FindControl(字符串id)

按控件id对控件的子控件进行递归搜索


非常适合我。

谢谢大家的快速回答!!!顺便说一句,我们必须将if(container.ID==name)改为if(container.name==name)可能是因为.ID用于ASP.NET Web控件标识;而.Name用于Windows窗体控件标识。两组不同的控件。@Swoosh:这就是为什么我在注释中包含代码段:
粘贴代码段,以防链接消失