How to Search and Drag-Drop Nodes in a TreeView Control
One of our dotnetcurry.com viewers Tim-Ann, asked me two queries about the TreeView control. He wanted to search a node and then move that node within the TreeView control. I thought of dedicating an article on it.
In this article, we will explore how to drag and drop nodes in a Treeview control. We will also explore how to search nodes in a TreeView.
Step 1: Create a Windows Forms Application. Drag and drop a Button(btnSearch) and a TextBox(txtSearch) on to the form. Now add a TreeView(tvw) control to the form.
In the Form_Load() add the following code to populate the TreeView:
C#
private void Form1_Load(object sender, EventArgs e)
{
treeView1.BeginUpdate();
treeView1.Nodes.Add(".NET");
treeView1.Nodes[0].Nodes.Add("Programming");
treeView1.Nodes[0].Nodes[0].Nodes.Add("C#");
treeView1.Nodes[0].Nodes[0].Nodes.Add("VB.NET");
treeView1.Nodes.Add("Servers");
treeView1.Nodes[1].Nodes.Add("Windows Server");
treeView1.Nodes[1].Nodes[0].Nodes.Add("W2K3");
treeView1.Nodes[1].Nodes[0].Nodes.Add("W2K8");
treeView1.Nodes[1].Nodes.Add("SQL Server 2008");
treeView1.Nodes[1].Nodes.Add("Exchange Server");
// Expand all tree nodes when first loaded
treeView1.ExpandAll();
treeView1.EndUpdate();
treeView1.AllowDrop = true;
}
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
treeView1.BeginUpdate()
treeView1.Nodes.Add(".NET")
treeView1.Nodes(0).Nodes.Add("Programming")
treeView1.Nodes(0).Nodes(0).Nodes.Add("C#")
treeView1.Nodes(0).Nodes(0).Nodes.Add("VB.NET")
treeView1.Nodes.Add("Servers")
treeView1.Nodes(1).Nodes.Add("Windows Server")
treeView1.Nodes(1).Nodes(0).Nodes.Add("W2K3")
treeView1.Nodes(1).Nodes(0).Nodes.Add("W2K8")
treeView1.Nodes(1).Nodes.Add("SQL Server 2008")
treeView1.Nodes(1).Nodes.Add("Exchange Server")
' Expand all tree nodes when first loaded
treeView1.ExpandAll()
treeView1.EndUpdate()
treeView1.AllowDrop = True
End Sub
Step 2: We will first create the functionality to ‘Search’ a TreeView. On the btnSearch click event, add the following code:
C#
private void FindInTreeView(TreeNodeCollection tncoll, string strNode)
{
foreach (TreeNode tnode in tncoll)
{
if (tnode.Text.ToLower() == strNode.ToLower())
{
tnode.ForeColor = Color.Blue;
tnode.TreeView.SelectedNode = tnode;
}
else
{
tnode.BackColor = tnode.TreeView.BackColor;
}
FindInTreeView(tnode.Nodes, strNode);
}
}
VB.NET
Private Sub FindInTreeView(ByVal tncoll As TreeNodeCollection, ByVal strNode As String)
For Each tnode As TreeNode In tncoll
If tnode.Text.ToLower() = strNode.ToLower() Then
tnode.ForeColor = Color.Blue
tnode.TreeView.SelectedNode = tnode
Else
tnode.BackColor = tnode.TreeView.BackColor
End If
FindInTreeView(tnode.Nodes, strNode)
Next tnode
End Sub
The code above searches the nodes recursively. Using a ForEach loop, we traverse the nodes and then compare the search item with the treenode text. If it matches, we highlight the node with a different color.
Step 3: Let us now add functionality to drag and drop nodes in the TreeView. We would first need to register a few events as shown below:
C#
private void RegisterEvents()
{
this.tvw.DragDrop += new System.Windows.Forms.DragEventHandler(this.tvw_DragDrop);
this.tvw.DragEnter += new System.Windows.Forms.DragEventHandler(this.tvw_DragEnter);
this.tvw.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.tvw_ItemDrag);
}
VB.NET
Private Sub RegisterEvents()
AddHandler tvw.DragDrop, AddressOf tvw_DragDrop
AddHandler tvw.DragEnter, AddressOf tvw_DragEnter
AddHandler tvw.ItemDrag, AddressOf tvw_ItemDrag
End Sub
Note: Call the RegisterEvents() in your Form_Load.
The next step is to add functionality to these event handlers :
C#
private void tvw_ItemDrag(object sender, ItemDragEventArgs e)
{
tvw.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void tvw_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void tvw_DragDrop(object sender, DragEventArgs e)
{
Point loc = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode node = (TreeNode)e.Data.GetData(typeof(TreeNode));
TreeNode destNode = ((TreeView)sender).GetNodeAt(loc);
if (node.Parent == null)
node.TreeView.Nodes.Remove(node);
else
node.Parent.Nodes.Remove(node);
if (destNode.Parent == null)
destNode.TreeView.Nodes.Insert(destNode.Index + 1, node);
else
destNode.Parent.Nodes.Insert(destNode.Index + 1, node);
}
VB.NET
Private Sub tvw_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs)
tvw.DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
Private Sub tvw_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub
Private Sub tvw_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
Dim loc As Point = (CType(sender, TreeView)).PointToClient(New Point(e.X, e.Y))
Dim node As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
Dim destNode As TreeNode = (CType(sender, TreeView)).GetNodeAt(loc)
If node.Parent Is Nothing Then
node.TreeView.Nodes.Remove(node)
Else
node.Parent.Nodes.Remove(node)
End If
If destNode.Parent Is Nothing Then
destNode.TreeView.Nodes.Insert(destNode.Index + 1, node)
Else
destNode.Parent.Nodes.Insert(destNode.Index + 1, node)
End If
End Sub
As shown in the code above, the ItemDrag event is raised from the TreeView control as soon as the user starts to drag the tree node. The DoDragDrop method initiates the drag-and-drop procedure.
The DragEnter event occurs when the TreeNode object is dragged from one point of the TreeView control to another point within the TreeView control. In this event, you can check the validity of the drag operation.
Finally the DragDrop event occurs when the TreeNode object is dropped at the destination point of the TreeView control. The drop point is calculated and the TreeNode object is retrieved, that is to be added to the destination node. That is all the code that is needed to initiate a drag and drop. Just make sure that the ‘AllowDrop’ property on the TreeView is set to True.
In this article, we explored how to search nodes in a TreeView control. We also explored how to drag and drop nodes in a treeview control. I hope this article was useful and I thank you for viewing it.