在Microsoft.Windows.Forms newsgrop的一个帖子中,Brian Roder(Microsoft)给出如何通过处理DragEnter、ItemDrag和DragDrop事件来解决这个问题的VB.NET代码。以下是C#版本的示例。
代码:
private void treeView2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeNode newNode;
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point pt;
TreeNode destinationNode;
pt = treeView2.PointToClient(new Point(e.X, e.Y));
destinationNode = treeView2.GetNodeAt(pt);
newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if (!destinationNode.Equals(newNode))
{
//destinationNode.Nodes.Add(newNode.Clone());
destinationNode.Nodes.Add((TreeNode)newNode.Clone());
destinationNode.Expand();
//移除原始的节点
newNode.Remove();
}
}
}
private void treeView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void treeView2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}