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
Solved

VB.net Drop text after TreeView node dragged

Posted on 2012-04-03
4
606 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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