Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

Drag and drop node between two treeviews in VB.NET 2003

This is my first time to do the drag and drop nodes between viewtrees, I can find some guidance from Google or MSDN. Basically we have three events that get involved:

ItemDrag
DragEnter
DragDrop

The sample coding is for drag and drop between two listboxes, anyone has the experience of handling the drag and drop nodes between treeviews?  I will appreciate if anyone could give some helpful guidance.
0
cocoWang
Asked:
cocoWang
  • 3
1 Solution
 
S-TwilleyCommented:
Here's some code from a previous solution by RonaldBiemens:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21296797.html?query=drag&topics=866

Just ask if you need any more help/explanation
0
 
iboutchkineCommented:
'whithin 1 tv and between 2 tv
 Ensure that the TreeView can accept objects dragged onto it by setting the AllowDrop property to True.




Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents TreeView2 As System.Windows.Forms.TreeView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.TreeView2 = New System.Windows.Forms.TreeView
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(8, 8)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(200, 248)
        Me.TreeView1.TabIndex = 0
        '
        'TreeView2
        '
        Me.TreeView2.ImageIndex = -1
        Me.TreeView2.Location = New System.Drawing.Point(224, 8)
        Me.TreeView2.Name = "TreeView2"
        Me.TreeView2.SelectedImageIndex = -1
        Me.TreeView2.Size = New System.Drawing.Size(200, 248)
        Me.TreeView2.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(432, 262)
        Me.Controls.Add(Me.TreeView2)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "Drag Drop TreeView"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag, TreeView2.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, TreeView2.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, TreeView2.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.Move

    End Sub

    Public Sub TreeView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop, TreeView2.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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Allow the TreeViews to be the target of a DragDrop operation
        'and populate it with some random TreeNodes for the example
        TreeView1.AllowDrop = True
        TreeView2.AllowDrop = True
        TreeView1.HideSelection = False
        TreeView2.HideSelection = False
        AddRandomNodes(TreeView1)
        AddRandomNodes(TreeView2)

    End Sub

    'Counter to provide each randomly created node with a unique name
    Dim nodeCounter As Integer = 1

    Private Sub AddRandomNodes(ByVal treeView As TreeView)

        'Adds a random number of root TreeNode the specified TreeView and
        'adds a random number of TreeNodes to each root node

        Randomize()

        treeView.BeginUpdate()
        For i As Integer = 0 To CInt(Rnd() * 5)
            Dim rootNode As New TreeNode("Node " & nodeCounter.ToString)
            treeView.Nodes.Add(rootNode)
            nodeCounter += 1
            AddRandomNodes(rootNode)
        Next
        treeView.EndUpdate()

    End Sub

    Private Sub AddRandomNodes(ByVal parentNode As TreeNode)

        'Adds a random number of TreeNodes to the specified TreeNode
        'and recurses up to 5 ChildNode levels.

        Dim parents() As String = parentNode.FullPath.Split("\"c)
        If parents.Length = 5 Then Exit Sub

        For i As Integer = 0 To CInt(Rnd() * 5)
            Dim newNode As New TreeNode("Node " & nodeCounter.ToString)
            nodeCounter += 1
            parentNode.Nodes.Add(newNode)
            AddRandomNodes(newNode)
        Next

    End Sub

End Class
0
 
cocoWangAuthor Commented:
Thanks iboutchkine,

It does drag and drop the node, but it also carries the child nodes over, is there any way just drap and drop the node itself without carry the child nodes over?  Meanwhile how to save the new nodes structure to the database table?

Thanks for your help, I may have various kind of questions.
0
 
cocoWangAuthor Commented:
S-Twilley,

Do you know how can I use stored procedure to retrieve data from table and display treeviews and how to save the treeview nodes into table via stored procedure?  I certainly will increase the points and split the points between you and iboutchkine
or whoever helps on these issues.
0
 
cocoWangAuthor Commented:
iboutchkine,

I did not hear from you for about one week, assuming that there is no way to drag and drop a parent node without carry over all children nodes under this parent node, please confirm on this question. Thanks !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now