public void MoveNode(MidgetTreeNode node, MidgetTreeNode newParentNode) { // retrieve old parent MidgetTreeNode oldParentNode = (MidgetTreeNode)node.Parent; // make sure not just copying to the parent or reorganizing within the parent if (!newParentNode.Equals(node) && !newParentNode.Equals(node.Parent) && oldParentNode != null) { // if child is becoming the parent of it's parent foreach (TreeNode childNode in node.Nodes) { if (childNode.Equals(newParentNode)) { // clone old child and adjust it's parent newParentNode = (MidgetTreeNode)childNode.Clone(); oldParentNode.Nodes.Add(newParentNode); oldParentNode.ExpandAll(); // removce old child childNode.Remove(); break; } } // remove node from it's old location oldParentNode.Nodes.Remove(node); // add the node to it's new parent newParentNode.Nodes.Add(node); newParentNode.ExpandAll(); } }
public void MoveNode(MidgetTreeNode node, MidgetTreeNode newParentNode) { // retrieve old parent MidgetTreeNode oldParentNode = (MidgetTreeNode)node.Parent; // make sure not just copying to the parent or reorganizing within the parent if(!newParentNode.Equals(node) && !newParentNode.Equals(node.Parent) && oldParentNode != null) { // if child is becoming the parent of it's parent foreach (TreeNode childNode in node.Nodes) { if(childNode.Equals(newParentNode)) { // clone old child and adjust it's parent newParentNode = (MidgetTreeNode)childNode.Clone(); oldParentNode.Nodes.Add(newParentNode); oldParentNode.ExpandAll(); // removce old child childNode.Remove(); break; } } // remove node from it's old location oldParentNode.Nodes.Remove(node); // add the node to it's new parent newParentNode.Nodes.Add(node); newParentNode.ExpandAll(); } }
protected override void OnDragDrop(DragEventArgs drgevent) { string [] text = drgevent.Data.GetFormats(); // if the object being dragged can become a treenode if (drgevent.Data.GetDataPresent("MidgetUI.MidgetTreeNode", false)) { Point pt = PointToClient(new Point(drgevent.X, drgevent.Y)); // retrieve the new parent node MidgetTreeNode newParentNode = (MidgetTreeNode)GetNodeAt(pt); // if you have missed the list drop it under the root node if (newParentNode == null) { newParentNode = ((MidgetTreeNode)this.Nodes[0]); } // get newNode location MidgetTreeNode node = (MidgetUI.MidgetTreeNode)drgevent.Data.GetData("MidgetUI.MidgetTreeNode"); // retrieve old parent MidgetTreeNode oldParentNode = (MidgetTreeNode)node.Parent; // make sure not just copying to the parent or reorganizing within the parent if (!newParentNode.Equals(node) && !newParentNode.Equals(node.Parent) && oldParentNode != null) { // Issue update request if (NodeMoveRequest != null) { NodeMoveRequest(this, new NodeMoveRequestEventArgs(node, newParentNode)); } // no one is listening just do it else { MoveNode(node, newParentNode); } } } }