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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

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

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
Murray Brown
Asked:
Murray Brown
  • 4
  • 3
1 Solution
 
CodeCruiserCommented:
Is this code working for treeviews? Handle the DragEnter and DragDrop events of the textbox same as you did for treeview.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
CodeCruiserCommented:
Did you set the AllowDrop of textbox to true?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Yes
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
CodeCruiserCommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Your first post was right all along. Thanks very much. Error on my part
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now