Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Using Visual Basic.Net I have developed an application with a treeview associated with it. When the app loads it loads the treeview with all the pertinent information for both parent and there child elements. The child elements have the same displayname as the parent, except the child has the parent name and a "- child" after the parent name. I want the user to be able to left click on a child element and drag it over to another parent element in the treeview and upon release of the mouse have the child be renamed accordingly with its new parent's name. I am not quite sure how this would look in code. Any help or examples would be greatly appreciated.
  • 2 weeks later...
Posted

I found this in Internet, quite useful, doesn't need any modification anyway..

 

create a treeview named treeview1 and paste this code:

 

Public Class Form1

   Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.ItemDragEventArgs) _
       Handles TreeView1.ItemDrag

       'Set the drag node and initiate the DragDrop 
       DoDragDrop(e.Item, DragDropEffects.Move)

   End Sub

   Public Sub TreeView1_DragEnter(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles TreeView1.DragEnter

       'See if there is a TreeNode being dragged
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
           True) Then
           'TreeNode found allow move effect
           e.Effect = DragDropEffects.Move
       Else
           'No TreeNode found, prevent move
           e.Effect = DragDropEffects.None
       End If

   End Sub

   Public Sub TreeView1_DragOver(ByVal sender As System.Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver

       'Check that there is a TreeNode being dragged 
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub

       'Get the TreeView raising the event (incase multiple on form)
       Dim selectedTreeview As TreeView = CType(sender, TreeView)

       'As the mouse moves over nodes, provide feedback to 
       'the user by highlighting the node that is the 
       'current drop target
       Dim pt As Point = _
           CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
       Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)

       'See if the targetNode is currently selected, 
       'if so no need to validate again
       If Not (selectedTreeview.SelectedNode Is targetNode) Then
           'Select the    node currently under the cursor
           selectedTreeview.SelectedNode = targetNode

           'Check that the selected node is not the dropNode and
           'also that it is not a child of the dropNode and 
           'therefore an invalid target
           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

       'Currently selected node is a suitable target
       e.Effect = DragDropEffects.Move

   End Sub

   Public Sub TreeView1_DragDrop(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) _
    Handles TreeView1.DragDrop

       'Check that there is a TreeNode being dragged
       If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
             True) = False Then Exit Sub

       'Get the TreeView raising the event (incase multiple on form)
       Dim selectedTreeview As TreeView = CType(sender, TreeView)

       'Get the TreeNode being dragged
       Dim dropNode As TreeNode = _
             CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
             TreeNode)

       'The target node should be selected from the DragOver event
       Dim targetNode As TreeNode = selectedTreeview.SelectedNode

       'Remove the drop node from its current location
       dropNode.Remove()

       'If there is no targetNode add dropNode to the bottom of
       'the TreeView root nodes, otherwise add it to the end of
       'the dropNode child nodes
       If targetNode Is Nothing Then
           selectedTreeview.Nodes.Add(dropNode)
       Else
           targetNode.Nodes.Add(dropNode)
       End If

       'Ensure the newley created node is visible to
       'the user and select it
       dropNode.EnsureVisible()
       selectedTreeview.SelectedNode = dropNode

   End Sub
End Class

George C.K. Low

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...