Use Auto-scroll in Treeview Drag/Drop Operations

Provided by Dan Haught, FMS Executive Vice President

When you implement drag and drop in a treeview control, you need to support some type of auto-scroll functionality. For example, when you drag an item from a visible tree node, and the destination tree node is outside of the current view of the treeview, the control should automatically scroll up or down depending on the direction of the mouse.

The Windows Forms Treeview control does not include built-in functionality to accomplish this. However, it is fairly easy to implement this yourself.

Step 1: Get your treeview drag and drop code working

Make sure your treeview drag and drop code works correctly without autoscrolling. See the topics in this folder for more information on how to implement drag and drop in a treeview.


Step 2: Add a definition for the SendMessage function

In order to tell the treeview to scroll up or down, you need to call the Windows API SendMessage() function. To do this, add the following code the top of your class:

    // C#

    // Make sure you have the correct using clause to see DllImport:

    // using System.Runtime.InteropServices;

     [DllImport("user32.dll")]

        private static extern int SendMessage (IntPtr hWnd, int wMsg, int wParam, 

            int lParam);

   

     ' VB

     Private Declare Function SendMessage _

            Lib "user32" _

            Alias "SendMessageA" _

            (ByVal hwnd As IntPtr, _

            ByVal wMsg As Integer, _

            ByVal wParam As Integer, _

            ByVal lParam As Integer) _

            As Integer


Step 3: Hook into the DragScroll Event

In the DragScroll event, determine where the mouse cursor is in relation to the top and bottom of the treeview control. Then call SendMessage to scroll as apporpriate.

    ' VB

    ' Implement an "autoscroll" routine for drag

    ' and drop. If the drag cursor moves to the bottom

    ' or top of the treeview, call the Windows API

    ' SendMessage function to scroll up or down automatically.

    Private Sub DragScroll( _

        ByVal sender As Object, _

        ByVal e As DragEventArgs) _

        Handles TreeView1.DragOver

   

        ' Set a constant to define the autoscroll region

        Const ScrollRegion As Single = 20

   

        ' See where the cursor is

        Dim pt As Point = TreeView1.PointToClient(Cursor.Position)

   

        ' See if we need to scroll up or down

        If (pt.Y + ScrollRegion) > TreeView1.Height Then

   

            ' Call the API to scroll down

            SendMessage(TreeView1.Handle, 277&, 1&, vbNull)

   

        ElseIf (pt.Y) < TreeView1.Top + ScrollRegion Then

   

            ' Call thje API to scroll up

            SendMessage(TreeView1.Handle, 277&, 0&, vbNull)

   

        End If

   

    End Sub

 

    // C#

 

    // Implement an "autoscroll" routine for drag

    //  and drop. If the drag cursor moves to the bottom

    //  or top of the treeview, call the Windows API

    //  SendMessage function to scroll up or down automatically.

    private void DragScroll (

        object sender,

        DragEventArgs e)

    {

        // Set a constant to define the autoscroll region

        const Single scrollRegion = 20;

   

        // See where the cursor is

        Point pt =  TreeView1.PointToClient(Cursor.Position);

   

        // See if we need to scroll up or down

        if ((pt.Y + scrollRegion) > TreeView1.Height)

        {

            // Call the API to scroll down

            SendMessage(TreeView1.Handle, (int)277, (int)1, 0);

        }

        else if (pt.Y < (TreeView1.Top + scrollRegion))

        {

            // Call thje API to scroll up

            SendMessage(TreeView1.Handle, (int)277, (int)0, 0);

    }


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS