Unable to delete node from treeview

3 posts, 0 answers
  1. AccSys
    AccSys avatar
    9 posts
    Member since:
    May 2011

    Posted 20 May 2011 Link to this post

    Hi,

    This seems like the simplest thing in the world to do, but I have been unable (as yet) to simply remove a treenode from the TreeView control.  I've created a RadForm with JUST a RadTreeView on it, added a handful of items, then simply responded to the SelectedNodeChanged() event to delete the node clicked.  This isn't what it's going to do in real life, but it's a simple test.  It doesn't work if I do the delete from a button event etc.

    Here's the list of items:
    treeMenu.AllowRemove = true;
    treeMenu.NodeRemoved += new RadTreeView.RadTreeViewEventHandler(treeMenu_NodeRemoved);
    RadTreeNode rootNode = new RadTreeNode("Menu",true);
    RadTreeNode homeMenu = new RadTreeNode("Home",true);
    RadTreeNode servicesMenu = new RadTreeNode("Our Services",true);
     
    servicesMenu.Nodes.Add(new RadTreeNode("Compliance Services",true));
    servicesMenu.Nodes.Add(new RadTreeNode("Support Services",true));
    servicesMenu.Nodes.Add(new RadTreeNode("Consultancy & Systems Advice",true));
    servicesMenu.Nodes.Add(new RadTreeNode("Business Startup",true));
     
    rootNode.Nodes.Add(homeMenu);
    rootNode.Nodes.Add(servicesMenu);
     
    treeMenu.Nodes.Add(rootNode);

    Then the event to delete the selected node:
    private void treeMenu_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
    {
        treeMenu.Nodes.Remove(e.Node);
    }

    I've set AllowRemove to true in the Design Interface and in the code, but nothing.  I've hooked the NodeRemoved() event to see if it's actually doing it, but nothing. The Event treeMenu_SelectedNodeChanged() is firing fine, and I'm not seeing the NodeRemove() event at all (and nothing is removed from the tree).

    Help! This is just bugging me now!

    Thanks,
    Paul
  2. AccSys
    AccSys avatar
    9 posts
    Member since:
    May 2011

    Posted 20 May 2011 Link to this post

    Hi,

    Just to let you know what else I tried:
    treeMenu.SelectedNode.Remove();

    Just throws an exception of "The method or operation is not implemented" but DOES delete the node from the tree.

    What's going on?

    Thanks
    Paul
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Svett
    Admin
    Svett avatar
    728 posts

    Posted 25 May 2011 Link to this post

    Hello Paul,

    If you want to remove a node in the SelectedNodeChanged event, you should use the following code snippet:

    private void treeMenu_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
    {
        RadTreeNode parent = e.Node.Parent;
     
        if (parent != null)
        {
            parent.Nodes.Remove(e.Node);
        }
        else
        {
            treeMenu.Nodes.Remove(e.Node);
        }
    }

    You need to check whether the node is from first level or not. When you remove a concrete node, you should remove it from its parent Nodes collection.

    Regarding the second issue, I did not manage to reproduce it. I would kindly ask you to illustrate the exact scenario which causes it.

     

    Regards,
    Svett
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Back to Top