TreeView拖动
region TreeView拖动
private void tvFormel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode tn = e.Item as TreeNode;
//根节点不允许拖放操作
if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null))
{
this.tvFormel.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
}
//DoDragDrop(e.Item, DragDropEffects.Move);
}
private void tvFormel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void tvFormel_DragDrop(object sender, DragEventArgs e)
{
TreeNode NewNode;
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
DestinationNode.Expand();
//删除已经移动的节点
NewNode.Remove();
}
}
#endregion
private void tvFormel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode tn = e.Item as TreeNode;
//根节点不允许拖放操作
if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null))
{
this.tvFormel.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
}
//DoDragDrop(e.Item, DragDropEffects.Move);
}
private void tvFormel_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void tvFormel_DragDrop(object sender, DragEventArgs e)
{
TreeNode NewNode;
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
DestinationNode.Expand();
//删除已经移动的节点
NewNode.Remove();
}
}
#endregion