We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Removing childnodes from treeview

Thread ID:

Created:

Updated:

Platform:

Replies:

66582 Jul 31,2007 06:14 AM Aug 6,2007 07:22 PM Windows Forms 3
loading
Tags: Tools
hemanth
Asked On July 31, 2007 06:14 AM

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.

J.Nagarajan [Syncfusion]
Replied On August 1, 2007 06:42 PM

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



hemanth
Replied On August 2, 2007 07:10 AM


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.Nagarajan [Syncfusion]
Replied On August 6, 2007 07:22 PM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;