Drag and drop from treeview to textbox in vb.net

Drag and drop operation is the same as cut and paste (or copy and paste) through the mouse instead of the keyboard.

In drag and drop operation you need source and destination controls, source controls are those controls where you are cutting or copying and a target where you are pasting.
During this operation, a copy of the data is maintained in memory.

Drag and drop operation has these steps:

steps in a typical drag-and-drop operation:

1. Start draging process by calling the DoDragDrop method for the source control.

2. Set the AllowDrop property of target control set to True. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event like as :

3. As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.

4. If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.


Following example shows drag and drop functionality between treeview and textbox

Private Sub TreeView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
        If e.Button = MouseButtons.Right Then
            If TreeView1.SelectedNode IsNot Nothing Then
                TreeView1.DoDragDrop(TreeView1.SelectedNode.Text, DragDropEffects.Copy Or DragDropEffects.Move)
            End If
        End If
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        e.Effect = DragDropEffects.Copy
End Sub
Private Sub TextBox1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub