Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to drag an item from treeview node and number of movable picturebox should be created on windows form as we drag.

But in my programm, we can drag a parent node also, and only one picturebox is created.

Here is my code:

VB
Public Class Form1
    Dim pic As New PictureBox
    Public drag As Boolean = False
    Public mousex, mousey As Integer

    Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
        With pic
            .Size = New Size(50, 50)
            Me.Controls.Add(pic)
            .BackColor = Color.Black
        End With

        AddHandler pic.MouseDown, AddressOf pic_MouseDown
        AddHandler pic.MouseMove, AddressOf pic_Mousemove
        AddHandler pic.MouseUp, AddressOf pic_Mouseup
    End Sub

    Private Sub Form1_DragEnter1(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AllowDrop = True
    End Sub

    Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
                 True) = False Then Exit Sub

        Dim selectedTreeview As TreeView = CType(sender, TreeView)
        Dim pt As Point = _
            CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
        Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)

        If Not (selectedTreeview.SelectedNode Is targetNode) Then
            'Select the    node currently under the cursor
            selectedTreeview.SelectedNode = targetNode
            Dim dropNode As TreeNode = _
                CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                TreeNode)

            Do Until targetNode Is Nothing
                If targetNode Is dropNode Then
                    e.Effect = DragDropEffects.None
                    Exit Sub
                End If
                targetNode = targetNode.Parent
            Loop
        End If

        e.Effect = DragDropEffects.Copy
    End Sub

    Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
        DoDragDrop(e.Item, DragDropEffects.Copy)
    End Sub

    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Cursor = Cursors.Hand
            drag = True
            mousex = -e.X
            mousey = -e.Y
            Dim left As Integer = Me.PointToClient(MousePosition).X - sender.location.x
            Dim right As Integer = Me.PointToClient(MousePosition).Y - sender.location.y
            Dim width As Integer = Me.ClientSize.Width - (sender.width - left)
            Dim height As Integer = Me.ClientSize.Height - (sender.width - right)
            Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(left, right, width, height))
            sender.invalidate()
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            Me.Controls.Remove(pic)
        End If
    End Sub

    Private Sub pic_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            Dim mposition As New Point()
            mposition = Me.PointToClient(MousePosition)
            mposition.Offset(mousex, mousey)
            sender.location = mposition
        End If
    End Sub

    Private Sub pic_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
        Windows.Forms.Cursor.Clip = Nothing
        Me.Cursor = Cursors.Arrow
        sender.invalidate()
    End Sub
End Class
Posted
Updated 11-Jun-12 10:35am
v2
Comments
Maciej Los 11-Jun-12 16:36pm    
Where is a problem?
boys13 12-Jun-12 21:39pm    
i can't create a picturebox more than one on the form,& the parent node also be dragged.

1 solution

give a try to this, I have changed code in two events rest of the implementation is intact.

VB
 Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
     If (CType(e.Item, TreeNode).Parent Is Nothing) Then
         Dim nodes As TreeNodeCollection = CType(e.Item, TreeNode).Nodes
         For Each node As TreeNode In nodes
             DoDragDrop(node, DragDropEffects.Copy)
         Next
     Else
         DoDragDrop(e.Item, DragDropEffects.Copy)
     End If
 End Sub


Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
     pic = New PictureBox    ' every time create a new pictureBox control
     With pic
         .Size = New Size(100, 100)
         .BackColor = Color.Black
         Me.Controls.Add(pic)
     End With

     AddHandler pic.MouseDown, AddressOf pic_MouseDown
     AddHandler pic.MouseMove, AddressOf pic_Mousemove
     AddHandler pic.MouseUp, AddressOf pic_Mouseup
 End Sub
 
Share this answer
 
Comments
Shahan Ayyub 17-Jun-12 15:48pm    
More preferably, if nodes contains child node then a recursive lookup should be use like this: (only TreeView_ItemDrag event need to be changed)

Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
If (CType(e.Item, TreeNode).Parent Is Nothing) Then
Dim nodes As TreeNodeCollection = CType(e.Item, TreeNode).Nodes
DoRecursiveDragDrop(nodes)
Else
DoDragDrop(e.Item, DragDropEffects.Copy)
End If
End Sub

Private Sub DoRecursiveDragDrop(ByVal nodes As TreeNodeCollection)
For Each node As TreeNode In nodes
If (node.Nodes.Count > 1) Then
DoRecursiveDragDrop(node.Nodes)
Else
DoDragDrop(node, DragDropEffects.Copy)
End If
Next
End Sub
_Amy 31-Jul-12 2:22am    
My +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900