private void treeView1_DragDrop(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location.
TreeNode targetNode = treeView1.GetNodeAt(targetPoint); // Retrieve the node that was dragged.
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); // Confirm that the node at the drop location is not
// the dragged node or a descendant of the dragged node.
if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
{
// If it is a move operation, remove the node from its current
// location and add it to the node at the drop location.
if (e.Effect == DragDropEffects.Move)
{
draggedNode.Remove();
targetNode.PrevNode.Nodes.Add(draggedNode);
} // If it is a copy operation, clone the dragged node
// and add it to the node at the drop location.
else if (e.Effect == DragDropEffects.Copy)
{
targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
} // Expand the node at the location
// to show the dropped node.
targetNode.Expand();
} }可是上面的代码是把拖动的节点作为目标节点的子节点。
我把DropNode.Nodes.Add(DragNode);改为DropNode.Parent.Nodes.Add(DragNode);
能够拖动成为目标节点的兄弟节点,但是我想让他们的位置紧挨着,请高手指教。
因为如果目标节点如果有多个兄弟节点的话,拖动的节点就会成为目标节点的父节点的最后一个子节点。
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); // Retrieve the node at the drop location.
TreeNode targetNode = treeView1.GetNodeAt(targetPoint); // Retrieve the node that was dragged.
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); // Confirm that the node at the drop location is not
// the dragged node or a descendant of the dragged node.
if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
{
// If it is a move operation, remove the node from its current
// location and add it to the node at the drop location.
if (e.Effect == DragDropEffects.Move)
{
draggedNode.Remove();
targetNode.PrevNode.Nodes.Add(draggedNode);
} // If it is a copy operation, clone the dragged node
// and add it to the node at the drop location.
else if (e.Effect == DragDropEffects.Copy)
{
targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
} // Expand the node at the location
// to show the dropped node.
targetNode.Expand();
} }可是上面的代码是把拖动的节点作为目标节点的子节点。
我把DropNode.Nodes.Add(DragNode);改为DropNode.Parent.Nodes.Add(DragNode);
能够拖动成为目标节点的兄弟节点,但是我想让他们的位置紧挨着,请高手指教。
因为如果目标节点如果有多个兄弟节点的话,拖动的节点就会成为目标节点的父节点的最后一个子节点。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货