Community discussion forum

How to Hide TreeView Node?

  • 4 years ago
    Hi,

    I want to hide treeview particular node. Can you anybody help me?

    Thanks,
    Vijay
  • 4 years ago

    why dont you just remove the Item from the Tree Item collection, then re-add when your ready?


  • 4 years ago

    Thanks for your reply. I got an idea. But one small doubt. Can you tell me where can I store the node(best way) when I removed it from treeview?



    Regards,
    Vijay

  • 4 years ago

    a way to do it would be to group the nodes, then collapsae and expand when you want the nodes, so ive added nodes like this


    Code:

               //ADD NODES
               treeView1.Nodes.Clear();
               TreeNode t1 = treeView1.Nodes.Add("Top pn");
               t1.Nodes.Add("level 1");
               t1.Nodes.Add("level 1");
               t1.Nodes.Add("level 1");
               TreeNode t2 = t1.Nodes.Add("level 1");
               t2.Nodes.Add("level 1 - level 2");
               t2.Nodes.Add("level 1 - level 2");
               t2.Nodes.Add("level 1 - level 2");
               //SHOW ALL EXPANDED
               treeView1.ExpandAll();


    now i want to close the level 1  - level 2 nodes like


    Code:

               //Hide node
               treeView1.Nodes[0].Nodes[3].Nodes[0].Parent.Collapse();


    now to make sure they dont click those nodes out again, trap the click events and see if its the node being expanded like


    Code:

               TreeNode n = treeView1.Nodes[0].Nodes[3].Nodes[0].Parent;
               if (n == e.Node)
               {
                   //CANCEL EXPAND
                   e.Cancel=true;
                   MessageBox.Show("You cant access this at the  moment");
               }


    ovbiously you would have to have a bool variable running to see if it was aloud to beopened something like


    Code:

    bool LetOpen = false;
           private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
           {
               TreeNode n = treeView1.Nodes[0].Nodes[3].Nodes[0].Parent;
               if (n == e.Node&& LetOpen==false)
               {
                   //CANCEL EXPAND
                   e.Cancel=true;
                   MessageBox.Show("You cant access this at the  moment");
               }
           }

  • 2 years ago
    hi,
        i want to hide a particular node in a tree view on the occurrence of  a particular condition
    for eg..if(x=y) then show node
    is there any property of tree node which wud hide it or change visibility

    aditya .








  • 2 years ago

    TreeView1.Nodes.RemoveAt(i);

  • 2 years ago

    TreeView1.Nodes.RemoveAt(NodeIndex);

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!