C#完成TreeView節點拖拽的辦法。本站提示廣大學習愛好者:(C#完成TreeView節點拖拽的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成TreeView節點拖拽的辦法正文
本文實例講述了C#完成TreeView節點拖拽的辦法。分享給年夜家供年夜家參考。詳細以下:
public Form1() { InitializeComponent(); treeView1.AllowDrop = true; treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag); treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter); treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop); } void treeView1_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.treeView1.GetNodeAt(pt); //假如目的節點無子節點則添加為同級節點,反之添加到上級節點的未端 TreeNode NewMoveNode = (TreeNode)moveNode.Clone(); if (targeNode.Nodes.Count == 0) { targeNode.Parent.Nodes.Insert(targeNode.Index, NewMoveNode); } else { targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode); } //更新以後拖動的節點選擇 treeView1.SelectedNode = NewMoveNode; //睜開目的節點,便於顯示拖放後果 targeNode.Expand(); //移除拖放的節點 moveNode.Remove(); } void treeView1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode")) { e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } } void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { if (e.Button == MouseButtons.Left) { DoDragDrop(e.Item, DragDropEffects.Move); } }
願望本文所述對年夜家的C#法式設計有所贊助。