Removing childnodes from treeview

Hi,
I am trying to remove checked nodes from the tree view. I have gone through the forum and found that clone() will help. But even with clone i can delete only once the next time i try to delete the remaining nodes it is again showing error.

Please give a sample if possible of how to
1. Remove nodes using dragdrop which is a true tree.
2. Remove nodes using selected checkboxes (multiple)
3. Remove nodes using selectednodes(multiple)
4. Adding also using above three techniques.

Thanks in advance.

3 Replies

J. J.Nagarajan Syncfusion Team August 1, 2007 10:42 PM UTC

Hi Hemanth,

1. If you want to remove the the nodes using dragdrop then you can handle treeViewAdv_DragDrop event and you can remove the dropped node. Please refer to the following code snippet.

private void treeViewAdv_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewAdv treeView = sender as TreeViewAdv;

// Get the destination and source node.
TreeNodeAdv sourceNode = (TreeNodeAdv) e.Data.GetData(typeof(TreeNodeAdv));
treeView.SelectedNode = sourceNode;
this.treeViewAdv1.Nodes.Remove(sourceNode);
}

I have attached the sample that demonstrates this completely. In the same when you drag and drop the node to another loacation, the dropped node gets deleted. You can download the sample from the following page.

http://websamples.syncfusion.com/samples/Tools.Windows/TreeDragDrop/main.htm

Please let me know if you have any questions.

2. If you want to remove the checked nodes then you have to handle AfterCheck event of the treeview. Please refer to the following code snippet

private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
{
e.Node.Remove();
}

3. If you want to remove the selected nodes then you can write a delegate for NodeSelection event. I have attached the sample that demonstrates this completely. In this sample when you select the node by mouse click or by key selection, then the selection node gets deleted.

http://websamples.syncfusion.com/samples/Tools.Windows/RemoveNodes/main.htm

4.

a. If you want to add a node using dragdrop then you can handle treeViewAdv_DragDrop event and you can add the dropped node. Please refer to the following code snippet.

TreeNodeAdv sourceNode = (TreeNodeAdv) e.Data.GetData(typeof(TreeNodeAdv));
TreeNodeAdv destinationNode = this.treeViewDragHighlightTracker.HighlightNode;
TreeViewDropPositions dropPosition = this.treeViewDragHighlightTracker.DropPosition;

TreeNodeAdv Node = sourceNode.Clone();
Node.Move(destinationNode, NodePositions.Previous);
this.treeViewAdv1.Nodes.Add(Node);

Please refer to the attached sample that demonstrates this completely.

http://websamples.syncfusion.com/samples/Tools.Windows/AddNodes/main.htm

b. If you want to remove the checked nodes then you have to handle AfterCheck event of the treeview. Please refer to the following code snippet

private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
{
treeView1.Nodes.Add(e.Node);
}

c. If you want to add the selected nodes then you can write a delegate for NodeSelection event. I have attached the sample that demonstrates this completely. In this sample when you select the node by mouse click or by key selection, then the selection node gets added.

http://websamples.syncfusion.com/samples/Tools.Windows/AddSelectedNodes/main.htm

Please let me know if you have any questions.

Thanks,
Nagaraj




HE hemanth August 2, 2007 11:10 AM UTC


Hi Nagarajan,

Thanks for the reply, actually there were few modifications in my questions. I want to check multiple nodes and then i want to click a delete button so that it removes all the checked nodes, same way for selected.

Or else assume that you are moving nodes from treeview to listbox. There i want to select whatever the treenodes i want and then click a button to move the items to the list box.
Deleting a single node is quite simple.

Thanks in advance.

>Hi Hemanth,

1. If you want to remove the the nodes using dragdrop then you can handle treeViewAdv_DragDrop event and you can remove the dropped node. Please refer to the following code snippet.

private void treeViewAdv_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewAdv treeView = sender as TreeViewAdv;

// Get the destination and source node.
TreeNodeAdv sourceNode = (TreeNodeAdv) e.Data.GetData(typeof(TreeNodeAdv));
treeView.SelectedNode = sourceNode;
this.treeViewAdv1.Nodes.Remove(sourceNode);
}

I have attached the sample that demonstrates this completely. In the same when you drag and drop the node to another loacation, the dropped node gets deleted. You can download the sample from the following page.

http://websamples.syncfusion.com/samples/Tools.Windows/TreeDragDrop/main.htm

Please let me know if you have any questions.

2. If you want to remove the checked nodes then you have to handle AfterCheck event of the treeview. Please refer to the following code snippet

private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
{
e.Node.Remove();
}

3. If you want to remove the selected nodes then you can write a delegate for NodeSelection event. I have attached the sample that demonstrates this completely. In this sample when you select the node by mouse click or by key selection, then the selection node gets deleted.

http://websamples.syncfusion.com/samples/Tools.Windows/RemoveNodes/main.htm

4.

a. If you want to add a node using dragdrop then you can handle treeViewAdv_DragDrop event and you can add the dropped node. Please refer to the following code snippet.

TreeNodeAdv sourceNode = (TreeNodeAdv) e.Data.GetData(typeof(TreeNodeAdv));
TreeNodeAdv destinationNode = this.treeViewDragHighlightTracker.HighlightNode;
TreeViewDropPositions dropPosition = this.treeViewDragHighlightTracker.DropPosition;

TreeNodeAdv Node = sourceNode.Clone();
Node.Move(destinationNode, NodePositions.Previous);
this.treeViewAdv1.Nodes.Add(Node);

Please refer to the attached sample that demonstrates this completely.

http://websamples.syncfusion.com/samples/Tools.Windows/AddNodes/main.htm

b. If you want to remove the checked nodes then you have to handle AfterCheck event of the treeview. Please refer to the following code snippet

private void treeViewAdv1_AfterCheck(object sender, Syncfusion.Windows.Forms.Tools.TreeNodeAdvEventArgs e)
{
treeView1.Nodes.Add(e.Node);
}

c. If you want to add the selected nodes then you can write a delegate for NodeSelection event. I have attached the sample that demonstrates this completely. In this sample when you select the node by mouse click or by key selection, then the selection node gets added.

http://websamples.syncfusion.com/samples/Tools.Windows/AddSelectedNodes/main.htm

Please let me know if you have any questions.

Thanks,
Nagaraj




J. J.Nagarajan Syncfusion Team August 6, 2007 11:22 PM UTC

Hi Hamanth,

Sorry for the delay in getting back to you.

1. If you want to remove the CheckedNodes then you have to use the following code snippet

private void button1_Click(object sender, EventArgs e)
{

for (int i = 0; i < this.treeViewAdv1.CheckedNodes.Count; i++)
{
TreeNodeAdv node = this.treeViewAdv1.CheckedNodes[i];

RemoveNode(node, this.treeViewAdv1.Nodes);
}
}


private void RemoveNode(TreeNodeAdv fnode, TreeNodeAdvCollection Nodes)
{
for(int i = 0 ; i< Nodes.Count; i++)
{
TreeNodeAdv node = Nodes[i];
if (node.Text == fnode.Text)
{
Nodes.Remove(node);
}
if ( node.HasChildren )
RemoveNode(fnode , node.Nodes);
}
}

2. If you want to remove the selected nodes , then you have to use the following code snippet

private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.treeViewAdv1.SelectedNodes.Count; i++)
{
TreeNodeAdv node = this.treeViewAdv1.SelectedNodes[i];

RemoveSelectedNode(node, this.treeViewAdv1.Nodes);
}
}
private void RemoveSelectedNode(TreeNodeAdv fnode, TreeNodeAdvCollection SelectedNodes)
{
for (int i = 0; i < SelectedNodes.Count; i++)
{
TreeNodeAdv node = SelectedNodes[i];
if (node.Text == fnode.Text)
{
SelectedNodes.Remove(node);
}
if (node.HasChildren)
RemoveNode(fnode, node.Nodes);
}
}

I have attached the sample that demonstrates this completely. You can download the sample from the following page.

http://websamples.syncfusion.com/samples/Tools.Windows/F66582_Aug6/main.htm

Please refer to the sample and let me know if you have any questions.

Regards,
Nagaraj

Loader.
Up arrow icon