Results 1 to 11 of 11

Thread: [RESOLVED] TreeView Drag and Drop Node Placement

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    13

    Resolved [RESOLVED] TreeView Drag and Drop Node Placement

    I have a VB application with two TreeViews. I can Drag and Drop from one treeview to the other. I can prevent a drop on particular nodes. I can allow a node name change.

    What I need .....

    I need the easiest way to allow a node to be dropped between nodes. In other words, I need to allow the ability for my users to be able to reorder the nodes the way they need to. Whether it's a single node, or moving an entire parent/child group. Showing a line to indicate where the dragged node will drop would be great as well.

    Can someone please help me?

    Thanks!
    bmw

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: TreeView Drag and Drop Node Placement

    Here is a TreeView I wrote about a year ago that allows you to re-order nodes by drag-drop. Its been a while since I played with this so I can't remember if it has any kinks. Try out it. It may give you ideas:-
    vbnet Code:
    1. Option Strict On
    2.  
    3. Imports System.ComponentModel
    4.  
    5. Public Class TreeViewDraggableNodes
    6.     Inherits TreeView
    7.  
    8. #Region "Events"
    9.     Public Event NodeMovedByDrag As EventHandler(Of NodeMovedByDragEventArgs)
    10.     Protected Overridable Sub OnNodeMovedByDrag(ByVal e As NodeMovedByDragEventArgs)
    11.         RaiseEvent NodeMovedByDrag(Me, e)
    12.     End Sub
    13.  
    14.     Public Event NodeMovingByDrag As EventHandler(Of NodeMovingByDragEventArgs)
    15.     Protected Overridable Sub OnNodeMovingByDrag(ByVal e As NodeMovingByDragEventArgs)
    16.         RaiseEvent NodeMovingByDrag(Me, e)
    17.     End Sub
    18.  
    19.     Public Event NodeDraggingOver As EventHandler(Of NodeDraggingOverEventArgs)
    20.     Protected Overridable Sub OnNodeDraggingOver(ByVal e As NodeDraggingOverEventArgs)
    21.         RaiseEvent NodeDraggingOver(Me, e)
    22.     End Sub
    23.  
    24. #End Region
    25.  
    26.     Sub New()
    27.         MyBase.AllowDrop = True
    28.         'MyBase.DrawMode = TreeViewDrawMode.OwnerDrawAll
    29.     End Sub
    30.  
    31.     <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    32.     Public Shadows ReadOnly Property DrawMode() As System.Windows.Forms.TreeViewDrawMode
    33.         Get
    34.             Return MyBase.DrawMode
    35.         End Get
    36.     End Property
    37.  
    38.     '<DefaultValue(True)> _
    39.     <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    40.     Public Shadows ReadOnly Property AllowDrop() As Boolean
    41.         Get
    42.             Return MyBase.AllowDrop
    43.         End Get
    44.     End Property
    45.  
    46.     Protected Overrides Sub OnItemDrag(ByVal e As System.Windows.Forms.ItemDragEventArgs)
    47.         MyBase.DoDragDrop(e.Item, DragDropEffects.Move)
    48.  
    49.         MyBase.OnItemDrag(e)
    50.     End Sub
    51.  
    52.     Protected Overrides Sub OnDragOver(ByVal drgevent As System.Windows.Forms.DragEventArgs)
    53.  
    54.         'Get the node we are currently over
    55.         'while dragging another node
    56.         Dim targetNode As TreeNode = MyBase.GetNodeAt(MyBase.PointToClient(New Point(drgevent.X, drgevent.Y)))
    57.  
    58.         'Get the node being dragged
    59.         Dim dragNode As TreeNode = FindNodeInDataObject(drgevent.Data)
    60.  
    61.         Dim eaDraggingOver As New NodeDraggingOverEventArgs(dragNode, targetNode)
    62.         OnNodeDraggingOver(eaDraggingOver)
    63.  
    64.         If eaDraggingOver.DropIsLegal = False Then
    65.             drgevent.Effect = DragDropEffects.None
    66.             Return
    67.         End If
    68.  
    69.  
    70.         'If we are not currently dragging over
    71.         'a node...
    72.         If targetNode Is Nothing Then
    73.  
    74.             'Let no node be selected
    75.             MyBase.SelectedNode = Nothing
    76.  
    77.             'Allow the move because its valid
    78.             'to drag a node over the TreeView itself
    79.             'the drop will place the node being dragged
    80.             'in the root
    81.             drgevent.Effect = DragDropEffects.Move
    82.  
    83.             'Get out
    84.             Return
    85.         End If
    86.  
    87.         'This would only be nothing if something is being
    88.         'dragged over the TreeView that isn't a node
    89.         If dragNode IsNot Nothing Then
    90.  
    91.             'Illegal to drop nodes inside their descendants
    92.             'Its not logical
    93.             If targetNode Is dragNode OrElse IsNodeDescendant(targetNode, dragNode) Then
    94.  
    95.                 'Prevents a drop
    96.                 drgevent.Effect = DragDropEffects.None
    97.             Else
    98.  
    99.                 'Allows a drop
    100.                 drgevent.Effect = DragDropEffects.Move
    101.                 MyBase.SelectedNode = targetNode
    102.             End If
    103.  
    104.         End If
    105.  
    106.         MyBase.OnDragOver(drgevent)
    107.     End Sub
    108.  
    109.     Protected Overrides Sub OnDragDrop(ByVal drgevent As System.Windows.Forms.DragEventArgs)
    110.         Dim dragNode As TreeNode = FindNodeInDataObject(drgevent.Data)
    111.  
    112.         If dragNode IsNot Nothing Then
    113.             'Dim dragNode As TreeNode = DirectCast(drgevent.Data.GetData(GetType(TreeNode)), TreeNode)
    114.  
    115.             'Get the parent of the node before moving
    116.             Dim prevParent As TreeNode = dragNode.Parent
    117.             Dim parentToBe As TreeNode = If(MyBase.SelectedNode Is Nothing, Nothing, MyBase.SelectedNode)
    118.  
    119.             Dim eaNodeMoving As New NodeMovingByDragEventArgs(dragNode, prevParent, parentToBe)
    120.  
    121.             OnNodeMovingByDrag(eaNodeMoving)
    122.  
    123.             If eaNodeMoving.CancelMove = False Then
    124.  
    125.                 dragNode.Remove()
    126.                 If MyBase.SelectedNode IsNot Nothing Then
    127.                     MyBase.SelectedNode.Nodes.Add(dragNode)
    128.                 Else
    129.                     MyBase.Nodes.Add(dragNode)
    130.                 End If
    131.                 OnNodeMovedByDrag(New NodeMovedByDragEventArgs(dragNode, prevParent))
    132.             End If
    133.  
    134.         End If
    135.  
    136.  
    137.         MyBase.OnDragDrop(drgevent)
    138.     End Sub
    139.  
    140.     Private Function IsNodeDescendant(ByVal node As TreeNode, ByVal potentialElder As TreeNode) As Boolean
    141.         Dim n As TreeNode
    142.  
    143.         If node Is Nothing OrElse potentialElder Is Nothing Then Return False
    144.  
    145.         Do
    146.             n = node.Parent
    147.  
    148.             If n IsNot Nothing Then
    149.                 If n Is potentialElder Then
    150.                     Return True
    151.                 Else
    152.                     node = n
    153.                 End If
    154.             End If
    155.         Loop Until n Is Nothing
    156.  
    157.         Return False
    158.     End Function
    159.  
    160.     Private Function FindNodeInDataObject(ByVal dataObject As IDataObject) As TreeNode
    161.  
    162.         For Each format As String In dataObject.GetFormats
    163.             Dim data As Object = dataObject.GetData(format)
    164.  
    165.             If GetType(TreeNode).IsAssignableFrom(data.GetType) Then
    166.                 Return DirectCast(data, TreeNode)
    167.             End If
    168.         Next
    169.  
    170.         Return Nothing
    171.     End Function
    172.  
    173. End Class
    174.  
    175. Public Class NodeDraggingOverEventArgs
    176.     Inherits EventArgs
    177.  
    178.     Private _DropLegal As Boolean
    179.     Private _MovingNode As TreeNode
    180.     Private _TargetNode As TreeNode
    181.  
    182.     Public Sub New(ByVal movingNode As TreeNode, ByVal targetNode As TreeNode)
    183.         _DropLegal = True
    184.         _MovingNode = movingNode
    185.         _TargetNode = targetNode
    186.     End Sub
    187.  
    188.     Public ReadOnly Property TargetNode() As TreeNode
    189.         Get
    190.             Return _TargetNode
    191.         End Get
    192.     End Property
    193.     Public ReadOnly Property MovingNode() As TreeNode
    194.         Get
    195.             Return _MovingNode
    196.         End Get
    197.     End Property
    198.  
    199.     'Use this to disallow a drop
    200.     Public Property DropIsLegal() As Boolean
    201.         Get
    202.             Return _DropLegal
    203.         End Get
    204.         Set(ByVal value As Boolean)
    205.             _DropLegal = value
    206.         End Set
    207.     End Property
    208.  
    209.  
    210. End Class
    211.  
    212. Public Class NodeMovingByDragEventArgs
    213.     Inherits EventArgs
    214.  
    215.     Private _MovingNode As TreeNode
    216.     Private _CurParent As TreeNode
    217.     Private _ParentToBe As TreeNode
    218.  
    219.     Private _CancelMove As Boolean
    220.  
    221.     Public Sub New(ByVal nodeMoving As TreeNode, ByVal prevParent As TreeNode, ByVal parentToBe As TreeNode)
    222.         _MovingNode = nodeMoving
    223.         _CurParent = prevParent
    224.         _ParentToBe = parentToBe
    225.     End Sub
    226.     Public Property CancelMove() As Boolean
    227.         Get
    228.             Return _cancelMove
    229.         End Get
    230.         Set(ByVal value As Boolean)
    231.             _CancelMove = value
    232.         End Set
    233.     End Property
    234.     Public ReadOnly Property MovingNode() As TreeNode
    235.         Get
    236.             Return _MovingNode
    237.         End Get
    238.     End Property
    239.     Public ReadOnly Property CurrentParent() As TreeNode
    240.         Get
    241.             Return _CurParent
    242.         End Get
    243.     End Property
    244.     Public ReadOnly Property ParentToBe() As TreeNode
    245.         Get
    246.             Return _ParentToBe
    247.         End Get
    248.     End Property
    249. End Class
    250.  
    251. Public Class NodeMovedByDragEventArgs
    252.     Inherits EventArgs
    253.  
    254.     Private _MovedNode As TreeNode
    255.     Private _PreviousParent As TreeNode
    256.  
    257.     Public Sub New(ByVal nodeMoved As TreeNode, ByVal prevParent As TreeNode)
    258.         _MovedNode = nodeMoved
    259.         _PreviousParent = prevParent
    260.     End Sub
    261.     Public ReadOnly Property MovedNode() As TreeNode
    262.         Get
    263.             Return _MovedNode
    264.         End Get
    265.     End Property
    266.     Public ReadOnly Property PreviousParent() As TreeNode
    267.         Get
    268.             Return _PreviousParent
    269.         End Get
    270.     End Property
    271.  
    272. End Class

    Put all of the above code inside its own code file.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    13

    Re: TreeView Drag and Drop Node Placement

    Thank you, Niya!! I have created the component class and the EventArgs classes. This may sound stupid, but how and where in my app would I implement these?

    Thanks again!
    bmw

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: TreeView Drag and Drop Node Placement

    Quote Originally Posted by BMWessel View Post
    Thank you, Niya!! I have created the component class and the EventArgs classes. This may sound stupid, but how and where in my app would I implement these?

    Thanks again!
    bmw
    Quote Originally Posted by Niya View Post
    Put all of the above code inside its own code file.
    .....
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    13

    Re: TreeView Drag and Drop Node Placement

    Thank you again for responding so quickly. Looking at your code, it seems that it does what my app already does. I can move my nodes to other nodes within my TreeView and it will add the "dragged" node to the "target" node just fine. However, it adds the "dragged" node to the END of the "target" nodes collection.

    What I want to do, is be able to drag a node, between two nodes and the "dragged" node becomes a sibling of the two nodes, but lies between them.

    Example - Original TreeView Structure

    Parent 1
    -- p1Child 1
    -- p1Child 2
    -- p1Child 3
    Parent 2
    -- p2Child 1
    -- p2Child 2
    -- p2Child 3

    Now, I want to be able to take node "p2Child 3" and move it between "p1Child 1" and p1Child 2" so it would look like

    Parent 1
    -- p1Child 1
    -- p2Child 3
    -- p1Child 2
    -- p1Child 3
    Parent 2
    -- p2Child 1
    -- p2Child 2

    Thanks!!
    bmw
    Last edited by BMWessel; Apr 17th, 2013 at 12:52 PM.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: TreeView Drag and Drop Node Placement

    Well you're going to have to sacrifice the ability to re-parent nodes. Your drag-drop functionality will either have to re-order or re-parent. It can't intuitively do both. If you need to re-order, you going to have to rethink the logic that makes the drag-drop work.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: TreeView Drag and Drop Node Placement

    @Niya... good work. there is an alternative, offering the user a choice of where to insert the dragged node:

    TreeViewDraggableNodes.zip

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: TreeView Drag and Drop Node Placement

    Quote Originally Posted by .paul. View Post
    @Niya... good work. there is an alternative, offering the user a choice of where to insert the dragged node:

    TreeViewDraggableNodes.zip
    Amazing addition! I hadn't considered that at all. It feels quite intuitive as well. I'm surprised you figured it out enough to alter it like that in so little time. Its been like a year since I wrote that and I was reluctant to touch it for fear of breaking it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    13

    Re: TreeView Drag and Drop Node Placement

    I seem to be unable to explain myself properly. As your program works well even with the changes made by .paul, I still do not think you understand what I am looking for. Your program pops up a context menu asking to select "Child" or "Sibling", which is great! However, when I select either one, the dragged node is "added" to the parent.nodes collection as the last "element/node". What I need to do is have the ability to take a node, and physically drop it BETWEEN two exising nodes as a sibling.

    So, if I have a ParentNode that contains ChildNode1, ChildNode2, ChildNode3, and ChildNode4; and I take ChildNode3 and drag it between ChildNode1 and ChildNode2, I do NOT want to "add" a child to ChildNode1 or ChildNode2, I want to insert ChildNode3 BETWEEN ChildNode1 and ChildNode2 to the Parent.Nodes list would then be ChildNode1, ChildNode3, ChildNode2, ChildNode4.

    Is there ANY code out there that can help me with this???

    Thanks,
    bmw

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: TreeView Drag and Drop Node Placement

    try this:

    Code:
    Protected Overrides Sub OnDragDrop(ByVal drgevent As System.Windows.Forms.DragEventArgs)
        Dim dragNode As TreeNode = FindNodeInDataObject(drgevent.Data)
    
        If dragNode IsNot Nothing Then
            'Dim dragNode As TreeNode = DirectCast(drgevent.Data.GetData(GetType(TreeNode)), TreeNode)
            'Get the parent of the node before moving
    
            If cms.Items.Count = 0 Then
                cms.Items.Add("Child", Nothing, AddressOf childClicked)
                cms.Items.Add("Sibling", Nothing, AddressOf siblingClicked)
                cms.Tag = drgevent
                cms.Show(drgevent.X, drgevent.Y)
                Return
            Else
                cms.Items.Clear()
            End If
    
            Dim prevParent As TreeNode = dragNode.Parent
            Dim parentToBe As TreeNode = If(MyBase.SelectedNode Is Nothing, Nothing, If(choice = "child", MyBase.SelectedNode, If(choice = "sibling", MyBase.SelectedNode.Parent, Nothing)))
            Dim eaNodeMoving As New NodeMovingByDragEventArgs(dragNode, prevParent, parentToBe)
    
            choice = ""
    
            OnNodeMovingByDrag(eaNodeMoving)
            If eaNodeMoving.CancelMove = False Then
    
                dragNode.Remove()
                If parentToBe IsNot Nothing Then
                    'parentToBe.Nodes.Add(dragNode)
                    parentToBe.Nodes.Insert(MyBase.SelectedNode.Index + 1, dragNode)
                Else
                    MyBase.Nodes.Add(dragNode)
                End If
    
                OnNodeMovedByDrag(New NodeMovedByDragEventArgs(dragNode, prevParent))
            End If
    
        End If
        MyBase.OnDragDrop(drgevent)
    
    End Sub
    one other improvement too:

    Code:
    Private Sub cms_Closed(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripDropDownClosedEventArgs) Handles cms.Closed
        If e.CloseReason <> ToolStripDropDownCloseReason.ItemClicked Then
            cms.Items.Clear()
        End If
    End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    13

    Re: TreeView Drag and Drop Node Placement

    Well, I have actually figured out how to insert between nodes without a context menu.

    First off, I replaced the MS TreeView Control with the Infragistics NetAdvantage UltraTree; setting the AllowDrop property = True; and the Override properties of NodeSpacingBefore and NodeSpacingAfter to both be 1 (a value of -1 will overlap the node bounds; a value of 0 allows the node bounds to reside next to eachother with no spacing between; anything above 1, will allow spacing between nodes)

    With that in mind, below is the code to enable node placement either between nodes or add as a child node.

    STILL LOOKING for a way to draw a line when the mouse is "dragged over" the space between the nodes.

    Code:
    Imports Infragistics.Win
    Imports Infragistics.Win.UltraWinTree
    
    Public Class Form1
    
        Inherits System.Windows.Forms.Form
    
        Private mDragNode As UltraTreeNode
        Private mDestNode As UltraTreeNode
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Populate UltraTree Controls
            Dim iRoot As Integer
            Dim iChild As Integer
    
            'Populate both Tree Views
            For iRoot = 1 To 10
    
                Dim rootNode As UltraTreeNode = New UltraTreeNode("Node " & iRoot.ToString())
    
                For iChild = 1 To 10
                    rootNode.Nodes.Add(rootNode.Text & " Child " & iChild.ToString())
                Next iChild
    
                sourceTree.Nodes.Add(rootNode)
    
                rootNode = Nothing
    
            Next iRoot
    
    
        End Sub
    
    
        Private Sub sourceTree_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sourceTree.DragDrop
    
            Dim pt As Point
            Dim destNode As UltraTreeNode
            Dim prevNode As UltraTreeNode
            Dim nextNode As UltraTreeNode
            Dim pointNode As UltraTreeNode
            Dim parentNode As UltraTreeNode
            Dim prevRect As Rectangle
            Dim nextRect As Rectangle
            Dim destRect As Rectangle
            Dim pointRec As Rectangle
            Dim pntY As Integer
    
            pt = sourceTree.PointToClient(New Point(e.X, e.Y))
            pointNode = sourceTree.GetNodeFromPoint(pt)
    
            If pointNode Is Nothing Then
                'Loop through, adding to the x,y coordinates until we locate a node
                Do
                    pt.X = pt.X + 1
                    pt.Y = pt.Y + 1
                    pointNode = sourceTree.GetNodeFromPoint(pt)
                Loop Until Not pointNode Is Nothing
    
            End If
    
            'Let's grab coordinates
            pointRec = pointNode.Bounds
            parentNode = pointNode.Parent
            prevNode = pointNode.PrevVisibleNode
            prevRect = prevNode.Bounds
            nextNode = pointNode.NextVisibleNode
            nextRect = nextNode.Bounds
            destNode = prevNode.NextVisibleNode
            destRect = destNode.Bounds
    
            'Accomodate for the node spacing before and after each node
            pntY = pt.Y - sourceTree.Override.NodeSpacingBefore
            pntY = pntY - sourceTree.Override.NodeSpacingAfter
    
            If ((pntY = destRect.Top) Or (pntY = destRect.Bottom)) Then
                destNode.Nodes.Add(mDragNode)
            ElseIf ((pntY > destRect.Top) And (pntY < destRect.Bottom)) Then
                destNode.Nodes.Add(mDragNode)
            ElseIf ((pntY > destRect.Bottom) And (pntY < nextRect.Top)) Then
                If parentNode Is Nothing Then
                    sourceTree.Nodes.Insert((destNode.Index + 1), mDragNode)
                Else
                    parentNode.Nodes.Insert((destNode.Index + 1), mDragNode)
                End If
            ElseIf ((pntY < destRect.Top) And (pntY > prevRect.Bottom)) Then
                If parentNode Is Nothing Then
                    sourceTree.Nodes.Insert((prevNode.Index + 1), mDragNode)
                Else
                    parentNode.Nodes.Insert((prevNode.Index + 1), mDragNode)
                End If
            End If
    
            'Be sure to Refresh the tree after a drag and drop
            sourceTree.Refresh()
    
        End Sub
    
        Private Sub sourceTree_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sourceTree.DragEnter
    
            e.Effect = DragDropEffects.Move
    
        End Sub
    
        Private Sub sourceTree_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles sourceTree.DragOver
    
            Dim pt As Point
            Dim utNode As UltraTreeNode
            Dim leftPos As Integer
            Dim rightPos As Integer
    
    
            pt = sourceTree.PointToClient(New Point(e.X, e.Y))
            utNode = sourceTree.GetNodeFromPoint(pt)
    
            If Not utNode Is Nothing Then
                leftPos = utNode.Bounds.Left
                rightPos = sourceTree.Width - 4
                utNode = sourceTree.ActiveNode
                utNode.Selected = True
            Else
                'draw line
    
            End If
    
        End Sub
    
    
        Private Sub sourceTree_SelectionDragStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles sourceTree.SelectionDragStart
    
            mDragNode = sourceTree.ActiveNode
            mDragNode.Remove()
            sourceTree.DoDragDrop(mDragNode, DragDropEffects.Move)
            mDragNode.Selected = False
    
        End Sub
    
    
    
    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width