Solved

VB.net Drop text after TreeView node dragged

Posted on 2012-04-03
4
598 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
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:murbro
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 500 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:murbro
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check if number is currency 15 31
Hide Tab Page 3 34
Make a border less form movable 2 23
Closing all open child forms. 4 38
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

21 Experts available now in Live!

Get 1:1 Help Now