Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net Drop text after TreeView node dragged

Posted on 2012-04-03
4
Medium Priority
?
639 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:Murray Brown
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37803155
>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:Murray Brown
ID: 37803199
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 2000 total points
ID: 37803209
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:Murray Brown
ID: 37803264
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

926 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