.net C# Treeview的Node拖动,Node节点上下移动
2007-10-10 13:34
615 查看
定义一个TreeView 名字为TVdepartment,设置allowdrop为True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//移动代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void MiMoveUp_Click(object sender, EventArgs e) //向上移动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode Node = TVdepartment.SelectedNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode PrevNode = Node.PrevNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (PrevNode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewNode = (TreeNode)Node.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Node.Parent == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.Nodes.Insert(PrevNode.Index, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Parent.Nodes.Insert(PrevNode.Index, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void MiMoveDown_Click(object sender, EventArgs e)//向下移动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode Node = TVdepartment.SelectedNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NextNode = Node.NextNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (NextNode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewNode = (TreeNode)Node.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Node.Parent == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.Nodes.Insert(NextNode.Index + 1, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Parent.Nodes.Insert(NextNode.Index + 1, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//拖动代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_ItemDrag(object sender, ItemDragEventArgs e)//左键拖动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Button == MouseButtons.Left)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoDragDrop(e.Item, DragDropEffects.Move);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_DragEnter(object sender, DragEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Effect = DragDropEffects.Move;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Effect = DragDropEffects.None;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_DragDrop(object sender, DragEventArgs e)//拖动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得拖放中的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//根据鼠标坐标确定要移动到的目标节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point pt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode targeNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode = this.TVdepartment.GetNodeAt(pt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewMoveNode = (TreeNode)moveNode.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (targeNode.Nodes.Count == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Parent.Nodes.Insert(targeNode.Index, NewMoveNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//更新当前拖动的节点选择
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewMoveNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//展开目标节点,便于显示拖放效果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Expand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//移除拖放的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
moveNode.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
本文参考了:http://epasser.aydc.com.cn/article/adp/2-10-1/RSS%E9%98%85%E8%AF%BB%E5%99%A8%E5%88%B6%E4%BD%9C%E4%B9%8BTreeView%E6%A0%91%E8%8A%82%E7%82%B9%E7%A7%BB%E5%8A%A8%E6%93%8D%E4%BD%9C%E7%9A%84%E5%AE%9E%E7%8E%B0_-_12802.html
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//移动代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void MiMoveUp_Click(object sender, EventArgs e) //向上移动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode Node = TVdepartment.SelectedNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode PrevNode = Node.PrevNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (PrevNode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewNode = (TreeNode)Node.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Node.Parent == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.Nodes.Insert(PrevNode.Index, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Parent.Nodes.Insert(PrevNode.Index, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void MiMoveDown_Click(object sender, EventArgs e)//向下移动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode Node = TVdepartment.SelectedNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NextNode = Node.NextNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (NextNode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewNode = (TreeNode)Node.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Node.Parent == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.Nodes.Insert(NextNode.Index + 1, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Parent.Nodes.Insert(NextNode.Index + 1, NewNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//拖动代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_ItemDrag(object sender, ItemDragEventArgs e)//左键拖动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Button == MouseButtons.Left)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoDragDrop(e.Item, DragDropEffects.Move);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_DragEnter(object sender, DragEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Effect = DragDropEffects.Move;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Effect = DragDropEffects.None;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void TVdepartment_DragDrop(object sender, DragEventArgs e)//拖动
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//获得拖放中的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//根据鼠标坐标确定要移动到的目标节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Point pt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode targeNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode = this.TVdepartment.GetNodeAt(pt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode NewMoveNode = (TreeNode)moveNode.Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (targeNode.Nodes.Count == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Parent.Nodes.Insert(targeNode.Index, NewMoveNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//更新当前拖动的节点选择
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TVdepartment.SelectedNode = NewMoveNode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//展开目标节点,便于显示拖放效果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
targeNode.Expand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//移除拖放的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
moveNode.Remove();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
本文参考了:http://epasser.aydc.com.cn/article/adp/2-10-1/RSS%E9%98%85%E8%AF%BB%E5%99%A8%E5%88%B6%E4%BD%9C%E4%B9%8BTreeView%E6%A0%91%E8%8A%82%E7%82%B9%E7%A7%BB%E5%8A%A8%E6%93%8D%E4%BD%9C%E7%9A%84%E5%AE%9E%E7%8E%B0_-_12802.html
相关文章推荐
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动收藏
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动
- .net C# Treeview的Node拖动,Node节点上下移动
- Treeview的Node拖动,Node节点上下移动
- C#实现treeview节点上下左右自由移动
- C#实现treeview节点上下左右自由移动
- .net C# listbox的上下移动,拖动排序,两个listbox相互拖动
- ASP.NET treeview 节点上下移动
- C# TreeView树节点上下移动
- 用键盘上下左右键和ctrl键移动treeview的treenode节点
- reeview的Node拖动,Node节点上下移动
- ASP.NET treeview 树控件的节点上下移动
- ASP.NET 2005 C#如何在TreeView中设置节点之间的间距,节点上下间距?
- Windows Form(.NET,C#)程序使用MFC控件(treeview)DLL,treeview上面的上下左右键不工作
- C# winform treeview node添加右键菜单并选中节点