在价值正斜杠FindNode方法的TreeView [英] FindNode method TreeView with Forward Slash in Value

查看:192
本文介绍了在价值正斜杠FindNode方法的TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树型控件。低于code用于寻找特定父节点。

I have a tree view control. The code below is used to find the specific parent node.

TreeView AllProductsTreeView;

TreeNode nodeFound = AllProductsTreeView.FindNode("AAA/Sensors");

AAA - 类别

传感器 - 子分类

这工作正常,但是当一个子类值包含正斜杠出现问题。

This works fine but the issue occurs when a subcategory value contains a forward slash.

TreeView AllProductsTreeView;

TreeNode nodeFound = AllProductsTreeView.FindNode("AAA/Sensors/Energy");

AAA - 类别

传感器/能源 - 子分类

在上述情况下,它返回nodeFound对象的空值。

In above situation it returns a null value for nodeFound object.

如何使用发现节点法正斜杠找到子类别。

How can I use find node method with a forward slash to find the subcategory.

先谢谢了!

推荐答案

字符/对 FindNode 特殊功能(主/子节点分化),并有似乎没有任何办法避免它。一种选择是不使用这个字符的节点名称都可以。如果您不想更改名称,你可以用一个自定义的补充内置的功能,如下面的code:

The character "/" has a special functionality for FindNode (main/child node differentiation) and there seems to not be any way to avoid it. One option would be not using this character for node names at all. If you don't want to change the names, you can complement the in-built functionality with a custom one, as shown in the code below:

string nodePath = "AAA/Sensors/Energy";
TreeNode nodeFound = null;
string[] temp = nodePath.Split('/');
if (temp.Length > 2)
{
    //More than one "/"
    TreeNode mainNode = AllProductsTreeView.FindNode(temp[0]);
    string childPath = nodePath.Substring(temp[0].Length + 1, nodePath.Length - temp[0].Length - 1);
    foreach (TreeNode childNode in mainNode.ChildNodes)
    {
        if (childNode.Value == childPath)
        {
            nodeFound = childNode;
            break;
        }
    }
}
else
{
    nodeFound = AllProductsTreeView.FindNode(nodePath);
}

如你所见,这code只依赖于 FindNode 时给定的名称中包含一个/或更小;在所有其他情况下,它提取主节点名称和所依托的属性(其中/字符不会触发任何特殊的外观通过其所有的孩子功能)。

As you can see, this code only relies on FindNode when the given name contains one "/" or less; in all the other cases, it extracts the main node name and looks through all its children by relying on the Value property (for which the "/" character does not trigger any special functionality).

澄清:在code以上可以应付任何情况(独立于数量/的子节点的名称),这验证任何下列结构:

CLARIFICATION: the code above can deal with any situation (independently upon the number of "/" in the name of the child node) which verifies any of the following structures:

Main_node_without_slashes/Child_node_containing_any_number_of_slashes
Main_node_without_slashes

在其主要节点包括斜杠你就必须更新此code,通过设置的方式告诉code时,/应被理解为主要独生子女分化时的情况下,作为名称的一部分。

In case of having main nodes including forward slashes you would have to update this code, by setting up a way to tell the code when the "/" should be understood as main-child differentiation and when as part of the name.

这篇关于在价值正斜杠FindNode方法的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆