Solved

VB.net Drop text after TreeView node dragged

Posted on 2012-04-03
4
585 Views
Last Modified: 2012-06-21
Hi. I am using the code below to drag a TreeView node onto both the same TreeView to link to another node (where a line is drawn between the two) as well as a DataGridView. I also want to be able to drop the text into an Excel spreadsheet without disrupting any of the others

    Private Sub TreeView_From_ItemDrag(sender As Object, e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView_From.ItemDrag
        'Set the drag node and initiate the DragDrop
        DoDragDrop(e.Item, DragDropEffects.Move Or DragDropEffects.Copy)
 
    End Sub
    Private Sub TreeView_From_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TreeView_From.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
            e.Effect = DragDropEffects.All

        Else
            'No TreeNode found, prevent move
            e.Effect = DragDropEffects.None
            Exit Sub
        End If
        'intFrom_X_Axis = e.X - 50
        intFrom_X_Axis = 1535
        'Check number of nodes and base position on this
        Dim intNumberJoins As Integer = CInt(Me.lblNumberJoins.Text)
        If intNumberJoins = 0 Then
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 1 Then
            intFrom_X_Axis = intFrom_X_Axis + 2
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 2 Then
            intFrom_X_Axis = intFrom_X_Axis + 4
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 3 Then
            intFrom_X_Axis = intFrom_X_Axis + 6
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 4 Then
            intFrom_X_Axis = intFrom_X_Axis + 8
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 5 Then
            intFrom_X_Axis = intFrom_X_Axis + 10
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 6 Then
            intFrom_X_Axis = intFrom_X_Axis + 12
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 7 Then
            intFrom_X_Axis = intFrom_X_Axis + 14
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        ElseIf intNumberJoins = 8 Then
            intFrom_X_Axis = intFrom_X_Axis + 16
            Me.lblNumberJoins.Text = CStr(intNumberJoins + 1)
        End If

        oFrom_Point = Me.TreeView_From.PointToClient(New Point(intFrom_X_Axis, e.Y))

    End Sub

    Private Sub TreeView_From_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TreeView_From.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

    Private Sub TreeView_From_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TreeView_From.DragDrop

        Dim oTableColumn1 As String
        Dim oTableColumn2 As String

        '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) '#######
            Exit Sub
        Else
            'targetNode.Nodes.Add(dropNode) '######
            targetNode.ForeColor = Color.DarkOrange
            dropNode.ForeColor = Color.DarkOrange
            'targetNode.Name
            'Point p = this.PointToScreen(pictureBox1.Location);
            'oTo_Point = Me.TreeView_From.PointToClient(New Point(e.X, e.Y))


            oTo_Point = Me.TreeView_From.PointToClient(New Point(intFrom_X_Axis, e.Y))
            'Add the join
            oTableColumn1 = "[" & targetNode.Parent.Text & "]" & "[" & targetNode.Text & "]"
            oTableColumn2 = "[" & dropNode.Parent.Text & "]" & "[" & dropNode.Text & "]"

            DGV_Joins.Rows.Add("Joined", oTableColumn1, oTableColumn2, "Joined")
        End If

        'Ensure the newley created node is visible to
        'the user and select it
        dropNode.EnsureVisible()
        selectedTreeview.SelectedNode = dropNode
        oDrawLine()
        'DrawDottedLine()
        ' oEraseLine()
    End Sub
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
>DoDragDrop(e.Item, DragDropEffects.Move Or DragDropEffects.Copy)

Instead of using e.Item, use e.Item.Text or e.Item.Name and Excel would be able to handle the drop for you.

You would need to change the DragDrop event code in the other treeview so that you use the text to create a node rather than adding the dropped node.
0
 

Author Comment

by:murbro
Comment Utility
but
   Private Sub TreeView_From_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TreeView_From.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) I get the position of the node in the following code
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
You would need to change

If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
               True) = False Then Exit Sub


and rest should be fine in this sub.

Change above to

If e.Data.GetDataPresent(DataFormats.StringFormat) = False Then Exit Sub
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks. Working well so far but I am still finding problems getting the X and Y points of the nodes to draw my line. Might post a further question. Thanks very much so far. Its drpping into my Excel cell perfectly
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now