How to force Treeview scrollbar thumb scroll up/down when dragging any node

Sudip Bhatt 2,276 Reputation points
2020-11-22T19:22:04.863+00:00

My treeview has huge node. i made possible to drag and drop the node. when i am on last node and try to drag that node to upward then treeview scrollbar not going up. so guide me how could i force treeview scroll bar go up when i will drag bottom nodes to upward direction and same way i want to drag treeview top node to downward direction then scroll bar should go down.

please help me with sample code.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,872 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-11-23T03:08:59.903+00:00

    Hi Sudip Bhatt,
    First, I use the following code to drag and drop the node what you have achieved.

    private void Form1_Load(object sender, EventArgs e)  
    {  
        this.treeView1.ItemDrag += new ItemDragEventHandler(this.treeView1_ItemDrag);  
        this.treeView1.DragDrop += new DragEventHandler(this.treeView1_DragDrop);  
        this.treeView1.DragEnter += new DragEventHandler(this.treeView1_DragEnter);  
    }  
    private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)  
    {  
        DoDragDrop(e.Item, DragDropEffects.Move);  
        var _selectedNode = (TreeNode)e.Item;  
    }  
    private void treeView1_DragEnter(object sender, DragEventArgs e)  
    {  
        e.Effect = DragDropEffects.Move;  
    }  
    private void treeView1_DragDrop(object sender, DragEventArgs e)  
    {  
        // Retrieve the client coordinates of the drop location.  
        Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));  
      
        // Retrieve the node at the drop location.  
        TreeNode targetNode = treeView1.GetNodeAt(targetPoint);  
      
        // Retrieve the node that was dragged.  
        TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));  
      
        // Confirm that the node at the drop location is not   
        // the dragged node and that target node isn't null  
        // (for example if you drag outside the control)  
        if (!draggedNode.Equals(targetNode) && targetNode != null)  
        {  
            // Remove the node from its current   
            // location and add it to the node at the drop location.  
            draggedNode.Remove();  
            targetNode.Nodes.Add(draggedNode);  
      
            // Expand the node at the location   
            // to show the dropped node.  
            targetNode.Expand();  
        }  
    }  
    

    Then in order to tell the treeview to scroll up or down, you need to call the Windows API SendMessage() function.
    And next, determine where the mouse cursor is in relation to the top and bottom of the treeview control in the DragScroll event. Then call SendMessage to scroll as apporpriate.
    Here is a code example you can refer to.

    public static class NativeMethods  
    {  
        [DllImport("user32.dll", CharSet = CharSet.Auto)]  
        internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);  
      
        public static void Scroll(this Control control)  
        {  
            var pt = control.PointToClient(Cursor.Position);  
      
            if ((pt.Y + 20) > control.Height)  
            {  
                // scroll down  
                SendMessage(control.Handle, 277, (IntPtr)1, (IntPtr)0);  
            }  
            else if (pt.Y < 20)  
            {  
                // scroll up  
                SendMessage(control.Handle, 277, (IntPtr)0, (IntPtr)0);  
            }  
        }  
    }  
    

    The test result:
    41781-1123.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful