TreeView Delete Node ASP tutorial. NET code
private bool Isin (treenodecollection tnodes,string s)
{
foreach (TreeNode td in Tnodes)
{
if (Td.name = = s) return true;
if (Td.nodes.count > 0)
if (Isin (Td.nodes, s)) return true;
}
return false;
}
Instance method
private void Addtreenode ()
{
Determine if there are duplicate names
if (Traversetreenode (string rename))
{
MessageBox.Show ("This node already exists!") "," hint ");
}
Else
{
Add a node (I don't know where you want to find it)
}
}
private bool Traversetreenode (string rename)
{
Whether there is a duplicate name between the Add node and the child node of the current node
if (This.treeroom.selectednode.nodes.count > 0)
{
foreach (TreeNode node in this.treeroom.selectednode.nodes)
{
if (rename = = Node.text)
return true;
}
return false;
}
Whether there is a duplicate name between the Add node and the sibling of the current node (if the parent node of the current node exists)
else if (This.treeroom.selectednode.parent.nodes.count > 0)
{
foreach (TreeNode node in this.treeroom.selectednode.parent.nodes)
{
if (Rename.compareto (node.text) = = 0)
return true;
}
return false;
}
}
Issue 2: Deleting a treeview node if there are child nodes is not allowed
private void Deltreenode ()
{
Determine if there are child nodes
if (this.treeroom.selectednode.nodes.count>0)
{
MessageBox.Show ("This node contains child nodes!") "," hint ");
}
Else
{
Remove the current node
This.treeroom.selectednode.remove ();
}
}
Add:
Question 1: Do not know you add the node name to determine whether in the whole tree, if that need to traverse the entire tree.
private bool Traversetreenode (string rename)
{
BOOL flag = FALSE;
foreach (TreeNode node in treeroom.nodes.nodes)
{
if (node.nodes.count>0)
{
Flag = Traversetreenode (rename);
if (flag)
return true;
}
Else
{
if (Rename.equals (Node.text))
return true;
}
}
return false;
}
//
Send a function to find node
<summary>
Traverses the tree control and finds the node with the text code. Using recursive algorithm
</summary>
<param name= "Nodes" > node set to find </param>
<param name= "Parentcode" > matching conditions, for node text</param>
<returns></returns>
Private TreeNode Findnodebycode (treenodecollection nodes, string code)
{
if (nodes.count = 0)
return null;
foreach (TreeNode node in nodes)
{
Determine if this node is a target
if (node.text.tostring () = = Code.trim ())
return node;
Whether this node has child nodes
TreeNode node_2;
if (node.childnodes.count!= 0)
{
Has child nodes, the recursive call begins
node_2 = Findnodebycode (node.childnodes, code);
}
Else
{
No child nodes
node_2 = null;
}
Returns the target if the target is found recursively
if (node_2!= null)
return node_2;
This node and the child nodes of the node (including all child nodes) do not contain the target, and the next node loop
}
Traversed all nodes, failed to find the target, had to return null
return null;
}
}