c# - FindNode method TreeView with Forward Slash in Value -
i have tree view control. code below used find specific parent node.
treeview allproductstreeview; treenode nodefound = allproductstreeview.findnode("aaa/sensors");
aaa - category
sensors - sub category
this works fine issue occurs when subcategory value contains forward slash.
treeview allproductstreeview; treenode nodefound = allproductstreeview.findnode("aaa/sensors/energy");
aaa - category
sensors/energy- sub category
in above situation returns null value nodefound object.
how can use find node method forward slash find subcategory.
thanks in advance!
the character "/" has special functionality findnode
(main/child node differentiation) , there seems not way avoid it. 1 option not using character node names @ all. if don't want change names, can complement in-built functionality custom one, shown in code below:
string nodepath = "aaa/sensors/energy"; treenode nodefound = null; string[] temp = nodepath.split('/'); if (temp.length > 2) { //more 1 "/" 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); }
as can see, code relies on findnode
when given name contains 1 "/" or less; in other cases, extracts main node name , looks through children relying on value
property (for "/" character not trigger special functionality).
clarification: code above can deal situation (independently upon number of "/" in name of child node) verifies of following structures:
main_node_without_slashes/child_node_containing_any_number_of_slashes main_node_without_slashes
in case of having main nodes including forward slashes have update code, setting way tell code when "/" should understood main-child differentiation , when part of name.
Comments
Post a Comment