VB.net TreeView拖放 - 替换项目

时间:2015-09-08 13:42:52

标签: replace drag-and-drop treeview

在同一树视图中轻松替换拖动的项目和目标项目是否可行?这是我的代码:

  Private Sub TV_ServerList_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles TV_ServerList.ItemDrag, TV_Playlistcomposer.ItemDrag
    'Set the drag node and initiate the DragDrop
    DoDragDrop(e.Item, DragDropEffects.Copy)
End Sub


Private Sub TV_ServerList_DragEnter(sender As Object, e As DragEventArgs) Handles TV_ServerList.DragEnter, TV_Playlistcomposer.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.Copy
    Else
        'No TreeNode found, prevent move
        e.Effect = DragDropEffects.Copy
    End If

End Sub

Private Sub TV_ServerList_DragOver(sender As Object, e As DragEventArgs) Handles TV_ServerList.DragOver, TV_Playlistcomposer.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 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, allow the move
    e.Effect = DragDropEffects.Copy

End Sub

Private Sub TV_ServerList_DragDrop(sender As Object, e As DragEventArgs) Handles TV_Playlistcomposer.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)
    Dim targetNode As TreeNode = selectedTreeview.SelectedNode

    If dropNode.Nodes.Count = 0 And targetNode.Nodes.Count = 0 And targetNode.Text(0) <> "#" Then
        Try

            Dim cat As TreeNode = dropNode
            dropNode = targetNode.Clone()
            targetNode = cat.Clone()
            'Ensure the newley created node is visible to the user and select it
            dropNode.EnsureVisible()
            selectedTreeview.SelectedNode = targetNode


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    ElseIf dropNode.Nodes.Count = 0 Then


        'The target node should be selected from the DragOver event
        If targetNode.Text(0) = "#" Then

            'Remove the drop node from its current location
            'dropNode.Remove()
            Dim addnode As TreeNode = dropNode.Clone()
            '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(addnode)
            Else


                targetNode.Nodes.Add(addnode)


            End If

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


        End If
    End If
End Sub

这里的目标是将没有孩子的项目从一个树视图移动到另一个树视图,只移动到以“#”开头的块。当项目被移动时,我希望能够改变它们的顺序,并通过拖放移动其他“#”块。我还需要能够改变“#”块的顺序

0 个答案:

没有答案