• TreeView 节点拖拽


     1 public Form1()
     2 {
     3  InitializeComponent();
     4  treeView1.AllowDrop = true;
     5  treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);
     6  treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
     7  treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);
     8 }
     9 void treeView1_DragDrop(object sender, DragEventArgs e)
    10 {
    11  TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
    12  //根据鼠标坐标确定要移动到的目标节点
    13  Point pt;
    14  TreeNode targeNode;
    15  pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
    16  targeNode = this.treeView1.GetNodeAt(pt);
    17  //如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端
    18  TreeNode NewMoveNode = (TreeNode)moveNode.Clone();
    19  if (targeNode.Nodes.Count == 0)
    20  {
    21   targeNode.Parent.Nodes.Insert(targeNode.Index, NewMoveNode);
    22  }
    23  else
    24  {
    25   targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode);
    26  }
    27  //更新当前拖动的节点选择
    28  treeView1.SelectedNode = NewMoveNode;
    29  //展开目标节点,便于显示拖放效果
    30  targeNode.Expand();
    31  //移除拖放的节点
    32  moveNode.Remove();
    33 }
    34 void treeView1_DragEnter(object sender, DragEventArgs e)
    35 {
    36  if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
    37  {
    38   e.Effect = DragDropEffects.Move;
    39  }
    40  else
    41  {
    42   e.Effect = DragDropEffects.None;
    43  }
    44 }
    45 void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
    46 {
    47  if (e.Button == MouseButtons.Left)
    48  {
    49   DoDragDrop(e.Item, DragDropEffects.Move);
    50  }
    51 }
  • 相关阅读:
    AGC023C Painting Machines
    LG3834 可持久化线段树1
    又是一天
    ARC103F Distance Sums
    CF1053E Euler tour
    UOJ22 外星人
    洛谷4248 AHOI2013差异 (后缀数组SA+单调栈)
    洛谷2408不同字串个数/SPOJ 694/705 (后缀数组SA)
    CF49E Common ancestor(dp+dp+dp)
    洛谷4051 JSOI2007 字符加密(SA)
  • 原文地址:https://www.cnblogs.com/liujianshe1990-/p/9590336.html
Copyright © 2020-2023  润新知