Solved

show line between siblings when drag/dropping items to indicate target in treeview

Posted on 2010-09-24
6
543 Views
Last Modified: 2013-11-26
During drag and drop of an item in a treeview:
How can our windows forms application show a line between siblings to indicate that the user can drop the item between siblings
0
Comment
Question by:joachim58
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33757362
Without more details...try something like the below out...

*It only allows "leaf nodes" to be dragged and they can only be dropped on other "leaf nodes".  It also assumes that the leaf nodes are NOT in the root of the TreeView.

Anyhoo...it probably has some quirks left in it but it may give you some ideas:
Public Class Form1



    Private LastNode As TreeNode



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TreeView1.AllowDrop = True

        TreeView1.HideSelection = False

        TreeView1.ExpandAll()

    End Sub



    Private Sub TreeView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Dim tn As TreeNode = TreeView1.GetNodeAt(e.X, e.Y)

            If Not IsNothing(tn) AndAlso tn.Nodes.Count = 0 Then

                LastNode = Nothing

                TreeView1.SelectedNode = tn

                Dim data As New DataObject("MyTreeNode", tn)

                TreeView1.DoDragDrop(data, DragDropEffects.Move)

            End If

        End If

    End Sub



    Private Sub TreeView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter

        e.Effect = IIf(e.Data.GetDataPresent("MyTreeNode"), DragDropEffects.All, DragDropEffects.None)

    End Sub



    Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver

        Dim tn As TreeNode = TreeView1.GetNodeAt(TreeView1.PointToClient(Cursor.Position))

        Dim sourceNode As TreeNode = e.Data.GetData("MyTreeNode")

        If Not IsNothing(tn) AndAlso tn.Nodes.Count = 0 AndAlso Not (tn Is sourceNode) Then

            If Not (tn Is LastNode) Then

                TreeView1.Refresh()

                Application.DoEvents()

                Using G As Graphics = TreeView1.CreateGraphics

                    G.DrawLine(Pens.Red, tn.Bounds.Left, tn.Bounds.Bottom, tn.Bounds.Right, tn.Bounds.Bottom)

                End Using

                LastNode = tn

            End If

            e.Effect = DragDropEffects.All

        Else

            e.Effect = DragDropEffects.None

            If Not IsNothing(LastNode) Then

                TreeView1.Refresh()

                Application.DoEvents()

            End If

            LastNode = Nothing

        End If

    End Sub



    Private Sub TreeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop

        Dim tn As TreeNode = e.Data.GetData("MyTreeNode")

        If Not IsNothing(LastNode) Then

            TreeView1.Nodes.Remove(tn)

            If Not IsNothing(LastNode.Parent) Then

                LastNode.Parent.Nodes.Insert(LastNode.Parent.Nodes.IndexOf(LastNode) + 1, tn)

            End If

        End If

    End Sub



End Class

Open in new window

0
 

Author Comment

by:joachim58
ID: 33813546
Thank for the idea and the code that handles a part of our problem.

We need a  solution where the "insert between siblings" is additional functionality on top of the normal drag / drop (where you drop nodes as subnodes). Do you think it will be possible to implement that together, and do you think it will be possible to enhance aour solution so we can drag nodes between nodes that are not leave nodes?
Or does your experience concerning treeview enhancements say: better solution would be for example: show a dialog and ask "drop as sibling or child?"
Thanks  
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33813954
I only put the leaf node limitation on there to make it easier on me.  I'm pretty sure it could be modified to handle other scenarios...

Just didn't want to have to deal with situations such as if you attempt to drag a grandparent into a grandchild, what is supposed to happen then?

Can you give an example of how "deep" the nodes can go and if there are any constraints on relationships between nodes (other than parent/child).
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:joachim58
ID: 33820929
What we'd like to have is advice if trying to add additional functionality to the treeview is easy or should better be avoided.

We implemented a visual feedback about where a node will be placed (child / sibling) by rendering a horizontal line between (="sibling") or a caret beneath (="as child") the nodes. But we noticed, that that changing "between siblings" and "as child" when the user moves the mouse ober nodes is not really a thing that the treeview designers had in mind.

We have 3 types of nodes: From "root" to "leave":
chapter -  structure as deep as necessary, here we have the "as child" or "as sibling" problem
articlegroup - must have a chapter as a parent
order number - must have an articlegroup as a parent




0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 33832083
If it was "easy" then it would be built-in already...   =\

So you have:

    Chapter
        ArticleGroup
            OrderNumber

Can you explain more about what can be moved to what?...or what cannot be moved to what?

I really don't know if this should be avoided or not...I'd have to really understand what your app does and how the data relates to each other!  Maybe there is a better way to allow the user to re-arrange the data?  Maybe there is a better control or combination of controls that can display the data?  I can't see the "big picture" so I can't really recommend anything...
0
 

Author Comment

by:joachim58
ID: 33887035
Sorry, i had an influenza and could not work on this task,
meanwhile the problem is solved.
thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now