C# Winform Treeview 按标签查找节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19226857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:27:32  来源:igfitidea点击:

Winform Treeview find node by tag

c#winformssearchtreeview

提问by user1548103

I have a treeview where the display member could possibly have duplicates, and the tag would not. Example:

我有一个树视图,其中显示成员可能有重复项,而标签不会。例子:

TreeNode node = new TreeNode(itemName);
node.Tag = itemID; //unique ID for the item
treeView1.Nodes.Add(node);

So, when searching, I know I can search by the itemName by using

因此,在搜索时,我知道我可以使用 itemName 进行搜索

treeView1.Nodes.Find(itemName, true);

But how could I go about searching via the tag? There's no definition for treeView1.Nodes.Where, so no linq for me :(

但是我怎么能通过标签进行搜索呢?treeView1.Nodes.Where 没有定义,所以我没有 linq :(

Any suggestions on how to search by the tag? :) Thank you!

有关如何按标签搜索的任何建议?:) 谢谢!

采纳答案by King King

Try this:

尝试这个:

var result = treeView1.Nodes.OfType<TreeNode>()
                            .FirstOrDefault(node=>node.Tag.Equals(itemID));

NOTE: Because you said your itemIDis unique, so you can use FirstOrDefaultto search for the unique item. If it's not found the resultwill be null.

注意:因为你说你itemID是独一无二的,所以你可以FirstOrDefault用来搜索独一无二的物品。如果没有找到,result将会是null.

UPDATE

更新

To search for all the nodes at all levels, you can try using some recursive method, like this:

要搜索所有级别的所有节点,您可以尝试使用一些递归方法,如下所示:

public TreeNode FromID(string itemId, TreeNode rootNode){
   foreach(TreeNode node in rootNode.Nodes){
     if(node.Tag.Equals(itemId)) return node;
     TreeNode next = FromID(itemId, node);
     if(next != null) return next;
   }
   return null;
}
//Usage    
TreeNode itemNode = null;
foreach(TreeNode node in treeView1.Nodes){
  itemNode = FromID(itemId, node);
  if(itemNode != null) break;
}

回答by Hamix

    public TreeNode GetNode(string name, TreeNode rootNode)
    {
        foreach (TreeNode node in rootNode.Nodes)
        {
            if (node.Name.Equals(name)) return node;
            TreeNode next = GetNode(name, node);
            if (next != null) return next;
        }
        return null;
    }

    public TreeNode GetNode(string name)
    {
        TreeNode itemNode = null;
        foreach (TreeNode node in treeViewPermission.Nodes)
        {
            if (node.Name.Equals(name)) return node;
            itemNode = GetNode(name, node);
            if (itemNode != null) break;
        }
        return itemNode;
    }

回答by Richard June

The Name property is probably a better answer for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.name(v=vs.110).aspx

Name 属性可能是一个更好的答案。 http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.name(v=vs.110).aspx

var itemNode = new TreeNode(itemName);
itemNode.Name = itemID.ToString();
treeView1.Nodes.Add(node);

var insertedNode = treeView1.Nodes.Find(itemID.ToString(), true);

I would say this is a much cleaner way to handle it.

我会说这是一种更清洁的处理方式。

回答by dynamiclynk

By Tag version of @Hamix's

通过@Hamix 的标签版本

        public TreeNode GetNode(object tag, TreeNode rootNode)
        {
            foreach (TreeNode node in rootNode.Nodes)
            {
                if (node.Tag.Equals(tag)) return node;

                //recursion
                var next = GetNode(tag, node);
                if (next != null) return next;
            }
            return null;
        }

        public TreeNode GetNode(object tag)
        {
            TreeNode itemNode = null;
            foreach (TreeNode node in _sourceTreeView.Nodes)
            {
                if (node.Tag.Equals(tag)) return node;

                itemNode = GetNode(tag, node);
                if (itemNode != null) break;
            }
            return itemNode;
        }