- private void TVdepartment_ItemDrag(object sender, ItemDragEventArgs e)//左键拖动
- {
- if (e.Button == MouseButtons.Left)
- {
- DoDragDrop(e.Item, DragDropEffects.Move);
- }
- }
-
- private void TVdepartment_DragEnter(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
- {
- e.Effect = DragDropEffects.Move;
- }
- else
- {
- e.Effect = DragDropEffects.None;
- }
- }
-
- private void TVdepartment_DragDrop(object sender, DragEventArgs e)//拖动
- { //获得拖放中的节点
- TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
- //根据鼠标坐标确定要移动到的目标节点
- Point pt;
- TreeNode targeNode;
- pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
- targeNode = this.TVdepartment.GetNodeAt(pt);
- //添加结点
- TreeNode NewMoveNode = (TreeNode)moveNode.Clone();
targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode);
-
- //更新当前拖动的节点选择
- TVdepartment.SelectedNode = NewMoveNode;
- //展开目标节点,便于显示拖放效果
- targeNode.Expand();
-
- //移除拖放的节点
- moveNode.Remove();
- }
注意:别忘记把TreeView控件的AllowDrop属性设为true