Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whilst using drag and drop, can I cause a Treeview to Expand the node over which the user hovers?

In brief:

Is there any built-in function in .Net 2.0 to Expand TreeNodes when hovered over whilst a drag and drop operation is in progress?

I'm using C# in Visual Studio 2005.

In more detail:

I've populated a Treeview control with a multi levelled, multinoded tree (think organisational chart or file/folder dialog) and I want to use drag and drop to move nodes within the tree.

The drag drop code works well, and I can drop onto any visible node, however I would like my control to behave like Windows Explorer does when dragging files over the folder pane. Specifically, I'd like each folder to open if hovered over for 1/2 second or so.

I've begun developing a solution using Threading and a Sleep method but I'm running into problems and wondered if there was something in place already, if not I will knuckle down and learn how to use threading (it's about time, but I was hoping to get this app out quickly)

Do I need to write my own code to handle Expanding a TreeNode when hovered over in drag-drop mode?

like image 402
G-. Avatar asked Nov 10 '09 17:11

G-.


People also ask

How many root nodes can a TreeView control have?

A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.

Which of the following property is used to set the height between nodes of TreeView?

The ItemHeight property is used to set the height of each tree node in control. The Scrollable property is used in the tree-view to display the scroll bar by setting the value in control.

How do I know if TreeView node is selected?

SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.


1 Answers

You can use the DragOver event; it fires repeatedly while you are dragging an object Opening after a delay can be done very easily with two extra variables that note the last object under the mouse and the time. No threading or other tricks required (lastDragDestination and lastDragDestinationTime in my example)

From my own code:

TreeNode lastDragDestination = null;
DateTime lastDragDestinationTime;

private void tvManager_DragOver(object sender, DragEventArgs e)
{
    IconObject dragDropObject = null;
    TreeNode dragDropNode = null;

    //always disallow by default
    e.Effect = DragDropEffects.None;

    //make sure we have data to transfer
    if (e.Data.GetDataPresent(typeof(TreeNode)))
    {
        dragDropNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
        dragDropObject = (IconObject)dragDropNode.Tag;
    }
    else if (e.Data.GetDataPresent(typeof(ListViewItem)))
    {
        ListViewItem temp (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        dragDropObject = (IconObject)temp.Tag;
    }

    if (dragDropObject != null)
    {
        TreeNode destinationNode = null;
        //get current location
        Point pt = new Point(e.X, e.Y);
        pt = tvManager.PointToClient(pt);
        destinationNode = tvManager.GetNodeAt(pt);
        if (destinationNode == null)
        {
            return;
        }

        //if we are on a new object, reset our timer
        //otherwise check to see if enough time has passed and expand the destination node
        if (destinationNode != lastDragDestination)
        {
            lastDragDestination = destinationNode;
            lastDragDestinationTime = DateTime.Now;
        }
        else
        {
            TimeSpan hoverTime = DateTime.Now.Subtract(lastDragDestinationTime);
            if (hoverTime.TotalSeconds > 2)
            {
                destinationNode.Expand();
            }
        }
    }
}
like image 75
Zack Avatar answered Sep 21 '22 05:09

Zack