Drag and drop Tree Node

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.SplitContainer splitContainer1;
  private System.Windows.Forms.TreeView treeOne;
  private System.Windows.Forms.TreeView treeTwo;
  public Form1() {
        InitializeComponent();
    TreeNode node = treeOne.Nodes.Add("A");
    node.Nodes.Add("A1");
    node.Nodes.Add("A2");
    node.Expand();

    node = treeTwo.Nodes.Add("B");
    node.Nodes.Add("B1");
    node.Nodes.Add("B2");
    node.Expand();

    treeTwo.AllowDrop = true;
    treeOne.AllowDrop = true;

  }
  private void tree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
    TreeView tree = (TreeView)sender;
    TreeNode node = tree.GetNodeAt(e.X, e.Y);
    tree.SelectedNode = node;

    if (node != null)
    {
      tree.DoDragDrop(node, DragDropEffects.Copy);
    }
  }
  private void tree_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
  {
    TreeView tree = (TreeView)sender;

    e.Effect = DragDropEffects.None;

    TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
    if (nodeSource != null)
    {
      if (nodeSource.TreeView != tree)
      {
        Point pt = new Point(e.X, e.Y);
        pt = tree.PointToClient(pt);
        TreeNode nodeTarget = tree.GetNodeAt(pt);
        if (nodeTarget != null)
        {
          e.Effect = DragDropEffects.Copy;
          tree.SelectedNode = nodeTarget;
        }
      }
    }
  }
  private void tree_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {
    TreeView tree = (TreeView)sender;
    Point pt = new Point(e.X, e.Y);
    pt = tree.PointToClient(pt);
    TreeNode nodeTarget = tree.GetNodeAt(pt);
    TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
    nodeTarget.Nodes.Add((TreeNode)nodeSource.Clone());
    nodeTarget.Expand();
  }

  private void InitializeComponent()
  {
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.treeOne = new System.Windows.Forms.TreeView();
        this.treeTwo = new System.Windows.Forms.TreeView();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.SuspendLayout();
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(0, 0);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // splitContainer1.Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.treeOne);
        // 
        // splitContainer1.Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.treeTwo);
        this.splitContainer1.Size = new System.Drawing.Size(456, 391);
        this.splitContainer1.SplitterDistance = 238;
        this.splitContainer1.TabIndex = 0;
        this.splitContainer1.Text = "splitContainer1";
        // 
        // treeOne
        // 
        this.treeOne.Dock = System.Windows.Forms.DockStyle.Left;
        this.treeOne.HideSelection = false;
        this.treeOne.Location = new System.Drawing.Point(0, 0);
        this.treeOne.Name = "treeOne";
        this.treeOne.Size = new System.Drawing.Size(236, 391);
        this.treeOne.TabIndex = 5;
        this.treeOne.DragDrop += new System.Windows.Forms.DragEventHandler(this.tree_DragDrop);
        this.treeOne.DragOver += new System.Windows.Forms.DragEventHandler(this.tree_DragOver);
        this.treeOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tree_MouseDown);
        // 
        // treeTwo
        // 
        this.treeTwo.Dock = System.Windows.Forms.DockStyle.Fill;
        this.treeTwo.Location = new System.Drawing.Point(0, 0);
        this.treeTwo.Name = "treeTwo";
        this.treeTwo.Size = new System.Drawing.Size(214, 391);
        this.treeTwo.TabIndex = 7;
        this.treeTwo.DragDrop += new System.Windows.Forms.DragEventHandler(this.tree_DragDrop);
        this.treeTwo.DragOver += new System.Windows.Forms.DragEventHandler(this.tree_DragOver);
        this.treeTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tree_MouseDown);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(456, 391);
        this.Controls.Add(this.splitContainer1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "TreeView Drag-And-Drop";
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel2.ResumeLayout(false);
        this.splitContainer1.ResumeLayout(false);
        this.ResumeLayout(false);

  }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}