Solved

VB.net Dragging TreeView node name into a TextBox on a different TaskPane

Posted on 2012-04-02
7
823 Views
Last Modified: 2012-04-02
Hi. I have two Taskpanes in my Excel Add-in project and am using the following code to drag between two TreeView nodes. I also need to drag the TreeView nodes' name into a TextBox on a different Taskpane. What code would I use behind the textBox


    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)
        'get coordinates on TreeView

    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
        oFrom_Point = Me.TreeView_From.PointToClient(New Point(e.X, 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



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

        End If

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

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37795517
Is this code working for treeviews? Handle the DragEnter and DragDrop events of the textbox same as you did for treeview.
0
 

Author Comment

by:murbro
ID: 37795577
Hi.

I tried that but nothing happens,
Then I tested with the following code and still nothing.

    Private Sub TextBox1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        Globals.ThisAddIn.Application.Cells(1, 1).value = "this is a test to see if drop code runs"
    End Sub

    Private Sub TextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        Try


            '(3) Check the format of the data being dropped.
            If (e.Data.GetDataPresent(DataFormats.Text)) Then
                ' Display the copy cursor.
                e.Effect = DragDropEffects.Copy
            Else
                ' Display the no-drop cursor.
                e.Effect = DragDropEffects.None
            End If
        Catch
            MsgBox("There was a problem trying to drag and drop...")
        End Try
    End Sub
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37795723
Did you set the AllowDrop of textbox to true?
0
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!

 

Author Comment

by:murbro
ID: 37796485
Yes
0
 

Author Comment

by:murbro
ID: 37796502
I set up another TextBox to see if I could drag from there and it worked. There is something about dragging from the TreeView that doesn't work. The other TextBox looked as follows

    Private Sub TextBox2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
        MouseIsDown = True
    End Sub

    Private Sub TextBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseMove
        If MouseIsDown Then
            ' Initiate dragging.

            TextBox2.DoDragDrop(TextBox2.Text, DragDropEffects.Copy)

        End If


        MouseIsDown = False
    End Sub
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37797069
So you have treeview setup to initiate the drag drop correctly yes? Do you see the dragging happening when you start dragging a node from treeview? Does it show the + sign when you bring it over textbox?
0
 

Author Closing Comment

by:murbro
ID: 37797282
Hi. Your first post was right all along. Thanks very much. Error on my part
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

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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