TreeView Drag or NoDrag Indicators

209 Views Asked by At

I have two TreeView controls on a form. What I need to do is indicate to the user when they are allowed to drag specific items from TreeView A to TreeView B by displaying different cursors.

How can I accomplish this ?

1

There are 1 best solutions below

0
On

You can try handling the MouseMove event, perform some hit testing to know which node is moved over, then change the cursor. The following code should do what you want, I suppose the nodes that can be dragged will have a Hand cursor, otherwise the Arrow cursor is shown.

HashSet<TreeNode> specialNodes = new HashSet<TreeNode>();
//MouseMove event handler for your treeView1
private void treeView1_MouseMove(object sender, MouseEventArgs e) {
   var ht = treeView1.HitTest(e.Location);
   if (specialNodes.Contains(ht.Node) && 
       ht.Location == TreeViewHitTestLocations.Label) {
       treeView1.Cursor = Cursors.Hand;
   }
   else treeView1.Cursor = Cursors.Arrow;
}
//Usage
//add some node first
specialNodes.Add(treeView1.Nodes[0]);
//Then try moving your mouse over the node 0

You have to add your nodes which need to be indicated as draggable to the hashset sepcialNodes, it's up to you. Using a hashset will help improve the performance in case you have a lot of nodes.

In case you don't know how to register the handler above with the event MouseMove, try adding this code to your Form constructor (after InitializeComponent):

treeView1.MouseMove += treeView1_MouseMove;