Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net Drop text after TreeView node dragged

Posted on 2012-04-03
4
Medium Priority
?
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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