⌘BABAKOV.NET Let's code!
100% [light] [ru]

Drag And Drop and Scroll in TreeView C#

Not long ago, I had to implement DragAndDrop for TreeView and was surprised to find that scrolling does not work out of the box (does not work at all, as it turned out, Microsoft acknowledges this as a problem and suggests solving it in the way described below).



So, the way to do it.



First, we import the SendMessage function from user32 - for this, we, of course, do not forget to include in the project (using System.Runtime.InteropServices;).

[DllImport("user32.dll")]
public static extern int SendMessage(
       int hWnd,      // handle to destination window
       uint Msg,       // message
       long wParam,  // first message parameter
       long lParam   // second message parameter
       );


We set two variables - they will indicate where we are moving in our implementation…

public bool tree_go_down = false;
public bool tree_go_up = false;


We create a timer (in our case, we simply throw it onto the form) and create an event for it:

private void timer1_Tick(object sender, EventArgs e)
    {
        if (tree_go_down == true) //Здесь отправляем сообщение на Scroll вниз
        {
            SendMessage(treeView1.Handle.ToInt32(), 277, 1, 0);
            
        }
        if (tree_go_up == true) //Здесь отправляем сообщение на Scroll вверх
        {
            SendMessage(treeView1.Handle.ToInt32(), 277, 0, 0);
        }
          
    }


On the DragOver event of our TreeView, we make this ugly thing, the essence of which is to change the scrolling speed when approaching the borders of our TreeView.

private void treeView1_DragOver(object sender, DragEventArgs e)
        {

            //Определяем где форма и начальные координаты.

            int top_y = this.Top+30;//30 - высота заголовка формы.
            int top_x = this.Left;


            //Выводим координатки вверх формы.
            this.Text = e.X.ToString() + ":" + e.Y.ToString() + " - "+top_x.ToString()+":"+top_y.ToString();

            
            //Скролл вверх.
            
            if (e.Y < 150 + top_y)
            {
                timer1.Interval = 100;
                tree_go_up = true;
                tree_go_down = false;
            }
            if (e.Y < 100 + top_y)
            {
                timer1.Interval = 50;
                tree_go_up = true;
                tree_go_down = false;
            }
            if (e.Y < 50 + top_y)
            {
                timer1.Interval = 25;
                tree_go_up = true;
                tree_go_down = false;
            }

            //Скролл вниз.
            if (e.Y > treeView1.Height - 150)
            {
                timer1.Interval = 100;
                tree_go_up = false;
                tree_go_down = true;
            }
            if (e.Y > treeView1.Height - 100)
            {
                timer1.Interval = 50;
                tree_go_up = false;
                tree_go_down = true;
            }
            if (e.Y > treeView1.Height - 50)
            {
                timer1.Interval = 25;
                tree_go_up = false;
                tree_go_down = true;
            }

            //Стоп машина!
            if (e.Y < treeView1.Height - 150)
            {
                tree_go_down = false;
            }
            if (e.Y > 150 + top_y)
            {
                tree_go_up = false;
            }



            e.Effect = DragDropEffects.Copy;

        }


We start Drag And Drop (start dragging an element).

TreeNode StartNode = new TreeNode();

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            timer1.Enabled = true;
            TreeNode aNode = (TreeNode)e.Item;
            StartNode = new TreeNode();
            StartNode.Text = aNode.Text;
            StartNode.Tag = aNode.Tag;
        
            StartNode = aNode; DoDragDrop(aNode, DragDropEffects.Copy);
        }


We do this when dropping the element (end of dragging an element).

private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            timer1.Enabled = false;
            TreeNode TargetNode = treeView1.GetNodeAt(e.X, e.Y-top_y);

           //Манипуляции с самим деревом.
           treeView1.SelectedNode = TargetNode;
           TreeNode node = new TreeNode();
           node.Tag = StartNode.Tag;
           node.Text = StartNode.Text;
           treeView1.SelectedNode.Nodes.Add(node);
           StartNode.Remove();
        }


That's it, it should work. You can download the example with the source code here: treeviewscroll.rar · 21 downloads

Files

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.