Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating Drag and Drop Events Using Tree View Control

Author: DevASP
Download Source Code : 521_DragAndDropTreeview.zip

In this article I will try to explain you how you can create drag and drop events from to tree view list control using Visual Basic.Net (VB.Net)

Create a new application using vb.net and drag a label, two buttons, two tree view controls and a list box control. Include some nodes on both tree view controls.

 

Include the following global variable

 

Private Position As New Point(0, 0)

 

Now double click on the Allow Drop button and write the following code on button click event

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TreeView1.AllowDrop = True

        TreeView2.AllowDrop = True

        ListBox1.AllowDrop = True

End Sub

 

Write the following code on stop button click event

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        TreeView1.AllowDrop = False

        TreeView2.AllowDrop = False

        ListBox1.AllowDrop = False

End Sub

 

Write the following code on treeView1 Item Drag event

 

Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag

        Dim strItem As String = e.Item.ToString()

        DoDragDrop(strItem, DragDropEffects.Copy Or DragDropEffects.Move)

    End Sub

 

Write the following code treeView1 Drag Enter event

 

Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter

        If e.Data.GetDataPresent(DataFormats.Text) Then

            e.Effect = DragDropEffects.Copy

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub

 

Write the following code on treeView2 Drag Enter event

 

Private Sub treeView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragEnter

        If e.Data.GetDataPresent(DataFormats.Text) Then

            e.Effect = DragDropEffects.Copy

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub

 

Write the following code on treeView2 Drag Drop event

 

Private Sub treeView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView2.DragDrop

        Dim temp As String = "asadd"

        Dim s As String = CStr(e.Data.GetData(temp.GetType()))

        s = s.Substring((s.IndexOf(":") + 1)).Trim()

        Position.X = e.X

        Position.Y = e.Y

        Position = TreeView2.PointToClient(Position)

        Dim DropNode As TreeNode = Me.TreeView2.GetNodeAt(Position)

        If Not (DropNode Is Nothing) Then

            Dim DragNode As New TreeNode(s)

            TreeView2.Nodes.Insert(DropNode.Index + 1, DragNode)

        End If

    End Sub

 

Write the following code on listBox1 Drag Enter event

 

Private Sub listBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter

        If e.Data.GetDataPresent(DataFormats.Text) Then

            e.Effect = DragDropEffects.Copy

        Else

            e.Effect = DragDropEffects.None

        End If

    End Sub

 

Write the following code on listBox1 Drag Drop event

 

Private Sub listBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop

        Dim dummy As String = "hello"

        Dim s As String = CStr(e.Data.GetData(dummy.GetType()))

        s = s.Substring((s.IndexOf(":") + 1)).Trim()

        Position.X = e.X

        Position.Y = e.Y

        Position = ListBox1.PointToClient(Position)

        ListBox1.Items.Add(s)

    End Sub

 

 

Article Comments
would like to know more about it

Posted on 12/16/2006 5:22:04 AM by weku

TreeView to ListBox Drag and Drop

Posted on 3/5/2007 3:13:30 AM by sakthivel

hi i want drag and drop in treeview using webapplications in asp.net..plz send me aspx aspx.cs pages code to my mail id.

Posted on 5/20/2008 1:34:24 PM by rajasekha reddy

Thanks,
its very good article

Posted on 3/11/2010 11:48:42 PM by Deepali Mali

I want to drage file notepad from any where into my text box,and then the textbox read that file from specific place. I know how to read file, but i don't know how to use drag drop event, what code i should use in this extention? Please share me.

Posted on 8/30/2010 8:15:06 PM by Soknet

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to pass values from one form to another in Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net