开发者

.NET 2.0 C# Treeview Drag/Drop within TreeNodes

I am interested in capturing a drag/drop event that will start with the user dragging an existing TreeNode somewhere within the TreeView. While the user is dragging the TreeNode around, I am int开发者_运维技巧erested in capturing when the node has been dragged between two tree nodes. When the user does this, I wanted to display a hash mark in-between the tree nodes to designate if the node would be dropped within the node as a child or as a sibling. This hash mark would display either: - underneath the destination node (to indicate the source node will be dropped as a child of the destination node OR - underneath the destination node to the left (to indicate the source node will be dropped as a sibling of the destination node), before or after...

I have made some headway using the DragOver event. I am calculating the mouse location and deriving what the top and bottom nodes are as I drag the mouse around..

        int threshold = 8;  //Joe(hack)
        Point mouseLocation = mouseLocation = treeViewConditions.PointToClient(new Point(e.X, e.Y - threshold));
        TreeNode topNode = treeViewConditions.GetNodeAt(mouseLocation);
        mouseLocation = treeViewConditions.PointToClient(new Point(e.X + threshold, e.Y));
        TreeNode bottomNode = treeViewConditions.GetNodeAt(mouseLocation);

        if (topNode != null && bottomNode == null)
        {
            textBoxDescription.Text = "handling top node";
        }
        else if (topNode == null && bottomNode != null)
        {
            textBoxDescription.Text = "handling bottom node";
        }
        else if (topNode != null && bottomNode != null)
        {
            if (topNode != bottomNode)
            {
                textBoxDescription.Text = "between!";
            }
            else if (topNode == bottomNode)
            {
            }
        }

However in doing this, it just feels dirty. I am wondering if anyone knew of a better way to do accomplish this.

Thanks a ton in advance!


Drawing the 'hash mark' is going to be the real problem. TreeView has a DrawMode property but its DrawItem event doesn't let you draw between the nodes.

You need to handle this by changing the cursor to indicate what is going to happen. Use the GiveFeedback event, set e.UseCustomCursors to false and assign Cursor.Current to a custom cursor that indicates the operation.


This article articulates the same issue and provides an approach somewhat similar to the one you are already following (with the exception that the thresholds are essentially percentages of the height of the tree node). Based on this, and the fact that when I was doing this before, that was the best approach I could find, I think you're basically on track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜