private void DragDropTreeView_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false) && _nodeMap != "") { var movingNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); var nodeIndexes = _nodeMap.Split('|'); var insertCollection = Nodes; for (var i = 0; i < nodeIndexes.Length - 1; i++) { insertCollection = insertCollection[Int32.Parse(nodeIndexes[i])].Nodes; } insertCollection.Insert(Int32.Parse(nodeIndexes[nodeIndexes.Length - 1]), (TreeNode)movingNode.Clone()); SelectedNode = insertCollection[Int32.Parse(nodeIndexes[nodeIndexes.Length - 1])]; // Remove the old ersion of the node movingNode.Remove(); // Expand if necessary if (SelectedNode.Nodes.Count > 0 && !SelectedNode.IsExpanded) { SelectedNode.Expand(); } // Fire the event (if there is a handler attached) var args = new DragDropReorganizeFinishedEventArgs { Node = SelectedNode }; if (DragDropReorganizeFinished != null) { DragDropReorganizeFinished(this, args); } } }
private void treeView_DragDropReorganizeFinished(object source, DragDropReorganizeFinishedEventArgs e) { // This event is fired when a node is dropped in its new place. // e.Node is the node that has been dropped. var parentDescription = e.Node.Parent != null ? "under '" + e.Node.Parent.Text + "'" : "at the top level"; // Update the status bar to show what happened statusLabel.Text = string.Format( "'{0}' has been positioned at index {1} {2}", e.Node.Text, e.Node.Index, parentDescription); }