Implement Treeview Drag/Drop

Provided by Dan Haught, FMS Executive Vice President

To get drag and drop to work in a Windows Forms Treeview control, there are several steps to take.

  1. Create an event handler for the ItemDrag event. This handler will call the DoDragDropmethod.
  2. Create an event handler for the DragEnter event. This handler will set visual effects.
  3. Create an event handler for the DragDrop event. This handler will implement your functionality for the actual drag drop event.
  4. Set the AllowDrag property on the treview control to True.

Note: To get drag and drop to work in a C# Windows Form application, you need to change the threading model from the default of MTAThread to STAThread. To do this, add the appropriate attribute to your Main method, like this:

    [STAThread]

    public static void Main()

    {

        // startup logic here

    }


Example

To use this code, create a new Windows Form, add a treeview control, and add the following code to the form’s class. Then hook the appropriate event handlers to the related methods.

    ' VB

    Public Sub TreeView1_ItemDrag( _

        ByVal sender As System.Object, _

        ByVal e As System.Windows.Forms.ItemDragEventArgs) _

        Handles treeview1.ItemDrag

 

        ' Initiate drag/drop

        DoDragDrop(e.Item, DragDropEffects.Move)

 

    End Sub

 

    Public Sub TreeView1_DragEnter( _

        ByVal sender As System.Object, _

        ByVal e As System.Windows.Forms.DragEventArgs) _

        Handles treeview1.DragEnter

 

        ' Set the visual effect

        e.Effect = DragDropEffects.Move

 

    End Sub

 

    Public Sub TreeView1_DragDrop( _

        ByVal sender As System.Object, _

        ByVal e As System.Windows.Forms.DragEventArgs) _

        Handles treeview1.DragDrop

        '

        ' Handle the Drag/Drop event.

 

        ' Create a new empty node

        Dim NewNode As TreeNode

 

        ' e contains the data of the dragged items. See if has a

        ' node data structure in it.

        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then

 

            ' node exists in Drag data payload

            Dim pt As Point

 

            ' Create a new node

            Dim DestinationNode As TreeNode

 

            ' Get the treeview point

            pt = treeview1.PointToClient(New Point(e.X, e.Y))

 

            ' Get a handle to the node the source node was dragged onto

            DestinationNode = treeview1.GetNodeAt(pt)

 

            ' Create a new node based on the data contained in the dragged node

            NewNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _

                            TreeNode)

 

            ' Check destination mode

            If Not DestinationNode.Equals(NewNode) Then

                ' Add a clone

                DestinationNode.Nodes.Add(CType(NewNode.Clone, TreeNode))

                ' Expand the new node

                DestinationNode.Expand()

                ' Remove the original node

                NewNode.Remove()

            End If

        End If

 

    End Sub

 

    // C#

    private void TreeviewItemDrag (

        object sender,

        ItemDragEventArgs e)

    {

        // Initiate drag/drop

        DoDragDrop (e.Item, DragDropEffects.Move);

    }

 

    private void TreeviewDragEnter (

    object sender,

    DragEventArgs e)

        {

    // Set the visual effect

    e.Effect = DragDropEffects.Move;

        }

 

    // Handle the dragdrop event

    public void TreeviewDragDrop (

    object sender,

    DragEventArgs e)

    {

        // e contains the data of the dragged items. See if has a

        // node data structure in it.

        if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", true))

        {

            // node exists in drag data payload

            Point pt = this.TreeView1.PointToClient(new Point (e.X, e.Y));

    

            // Get a handle to the destination node. This will become

            // the parent of our new node.

            TreeNode DestinationNode = this.TreeView1.GetNodeAt(pt);

   

            // Create the new node based on the dragged node

            TreeNode newNode = (TreeNode)     

            e.Data.GetData("System.Windows.Forms.TreeNode");

 

            // Check destination mode

            if (! (DestinationNode == newNode))

            {

                // add a clone

                DestinationNode.Nodes.Add ((TreeNode) newNode.Clone());

                // expand the new node

                DestinationNode.Expand();

                // remove the original node

                newNode.Remove();

            }

        }

    }

 

Now every time you start Visual Studio, the toolbar is ready for use.


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