How to check if root tree node has child nodes or not

I have a treeview with root root originally as Root. If I right-click on the Root node, I will have a context menu with some options like New and other, if I select New, I will add a node child to the Root node. If I right-click again on the Root node, and if Root has child nodes, I would like to clear all the child nodes and add a new node child, how can I do this

+3
source share
5 answers

After all the final answer

    if (tvwACH.HitTest(location).Node.Nodes.Count > 0 && tvwACH.SelectedNode.Parent == null )
        {
            foreach (TreeNode node in tvwACH.Nodes)
            {
                node.Nodes.Clear();
            }
    }
+1
source
+6

'right click', , Mouse Click, args TreeNodeMouseClickEventArgs node...

void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
      if (e.Button == MouseButtons.Right)
      {
        TreeNode selectedNode = e.Node;
      }
    }
0

TreeNode.Nodes. count , , <

0
                foreach (TreeNode node in treeview.Nodes)
                     {

                        if (node.ChildNodes.Count != 0)
                        {

                           //Node exists

                        }
                        else
                        {

                          //Node doesn't exists

                        }
                    }
-1

Source: https://habr.com/ru/post/1766201/


All Articles