Solved

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

Posted on 2012-04-02
7
804 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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 was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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