This is a migrated thread and some comments may be shown as answers.

Unable to delete node from treeview

2 Answers 273 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
AccSys
Top achievements
Rank 1
AccSys asked on 20 May 2011, 07:11 PM
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 Answers, 1 is accepted

Sort by
0
AccSys
Top achievements
Rank 1
answered on 20 May 2011, 08:21 PM
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
0
Svett
Telerik team
answered on 25 May 2011, 03:05 PM
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.
Tags
Treeview
Asked by
AccSys
Top achievements
Rank 1
Answers by
AccSys
Top achievements
Rank 1
Svett
Telerik team
Share this question
or