/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnDragDrop(System.Windows.Forms.DragEventArgs e) { // Custom cursor handling if (this._dragCursorType == DragCursorType.DragIcon) { this.Cursor = Cursors.Default; } this._formDrag.Visible = false; // Check it's a treenode being dragged if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { TreeNode dragNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); // Get the target node from the mouse coords Point pt = ((TreeView)this).PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = this.GetNodeAt(pt); if (targetNode == null) { return; } // De-color it targetNode.BackColor = SystemColors.HighlightText; targetNode.ForeColor = SystemColors.ControlText; TreeNode draggedOn = targetNode; int insertIndex = -1; if (DragCompleteValid != null) { DragCompletionValidEventArgs ea = new DragCompletionValidEventArgs(); ea.RemovedNode = dragNode; ea.NewParent = targetNode; ea.DraggedOn = draggedOn; ea.OldParent = dragNode.Parent; ea.Cancel = false; DragCompleteValid(this, ea); if (ea.Cancel) { return; } targetNode = ea.NewParent; draggedOn = ea.DraggedOn; insertIndex = ea.InsertIndex; } // 1) Check we're not dragging onto ourself // 2) Check we're not dragging onto one of our children // (this is the lazy way, will break if there are nodes with the same name, // but it's quicker than checking all nodes below is) // 3) Check we're not dragging onto our parent if (targetNode != dragNode && !targetNode.FullPath.StartsWith(dragNode.FullPath) && (dragNode.Parent != targetNode || (draggedOn != null && draggedOn.Parent == targetNode))) { // Copy the node, add as a child to the destination node TreeNode newTreeNode = (TreeNode)dragNode.Clone(); if (insertIndex == -1) { targetNode.Nodes.Add(newTreeNode); } else { targetNode.Nodes.Insert(insertIndex, newTreeNode); } targetNode.Expand(); TreeNode oldParent = dragNode.Parent; // Remove Original Node, set the dragged node as selected dragNode.Remove(); this.SelectedNode = newTreeNode; this.Cursor = Cursors.Default; // Call drag complete event if (this.DragComplete != null) { DragCompleteEventArgs ea = new DragCompleteEventArgs(); ea.RemovedNode = dragNode; ea.NewParent = targetNode; ea.OldParent = oldParent; ea.CloneNode = newTreeNode; ea.DraggedOn = draggedOn; ea.InsertIndex = insertIndex; this.DragComplete(this, ea); } } } }
private void projectView_DragCompleteValid(object sender, DragCompletionValidEventArgs e) { if (openProjectDir == null) { e.Cancel = true; return; } //Go up to nearest dir if (e.NewParent.Parent != null && !(e.NewParent.Tag is FolderItem && ((FolderItem)e.NewParent.Tag).Children.Count == 0)) e.NewParent = e.NewParent.Parent; while (true) { if (e.NewParent == null) { e.Cancel = true; return; } if (e.NewParent.Tag is FolderItem) break; e.NewParent = e.NewParent.Parent; } while (e.DraggedOn != null && (e.DraggedOn.Parent == null || e.DraggedOn.Parent.Tag != e.NewParent.Tag)) e.DraggedOn = e.DraggedOn.Parent; e.InsertIndex = e.DraggedOn == null ? -1 : e.NewParent.Nodes.IndexOf(e.DraggedOn); //If we are moving it down in same folder, we must add 1 to the index /*if (e.InsertIndex != -1 && e.NewParent == e.OldParent && e.NewParent.Nodes.IndexOf(e.RemovedNode) < e.InsertIndex) e.InsertIndex++;*/ DirItem draggedItem = (DirItem) e.RemovedNode.Tag; //If in output folder, only allow drag with same parrent if (draggedItem.IsDecendantOf(openProjectOutputDir) && e.NewParent != e.OldParent) { e.Cancel = true; return; } FolderItem targetFolder = (FolderItem) e.NewParent.Tag; if (targetFolder.Dir.GetFileSystemInfos().Any(item => item.FullName.TrimEnd('\\', '/') != draggedItem.FullName.TrimEnd('\\', '/') && item.Name == draggedItem.Text)) { MessageBox.Show(this, LocRM.GetString("Text20.Text"), LocRM.GetString("Error.Text")); e.Cancel = true; return; } }
/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnDragDrop(System.Windows.Forms.DragEventArgs e) { // Custom cursor handling if ( this._dragCursorType == DragCursorType.DragIcon ) { this.Cursor = Cursors.Default; } this._formDrag.Visible = false; // Check it's a treenode being dragged if( e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false) ) { TreeNode dragNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); // Get the target node from the mouse coords Point pt = ((TreeView)this).PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = this.GetNodeAt(pt); if (targetNode == null) return; // De-color it targetNode.BackColor = SystemColors.HighlightText; targetNode.ForeColor = SystemColors.ControlText; TreeNode draggedOn = targetNode; int insertIndex = -1; if (DragCompleteValid != null) { DragCompletionValidEventArgs ea = new DragCompletionValidEventArgs(); ea.RemovedNode = dragNode; ea.NewParent = targetNode; ea.DraggedOn = draggedOn; ea.OldParent = dragNode.Parent; ea.Cancel = false; DragCompleteValid(this, ea); if (ea.Cancel) return; targetNode = ea.NewParent; draggedOn = ea.DraggedOn; insertIndex = ea.InsertIndex; } // 1) Check we're not dragging onto ourself // 2) Check we're not dragging onto one of our children // (this is the lazy way, will break if there are nodes with the same name, // but it's quicker than checking all nodes below is) // 3) Check we're not dragging onto our parent if( targetNode != dragNode && !targetNode.FullPath.StartsWith(dragNode.FullPath) && (dragNode.Parent != targetNode || (draggedOn != null && draggedOn.Parent == targetNode))) { // Copy the node, add as a child to the destination node TreeNode newTreeNode = (TreeNode) dragNode.Clone(); if (insertIndex == -1) targetNode.Nodes.Add(newTreeNode); else targetNode.Nodes.Insert(insertIndex, newTreeNode); targetNode.Expand(); TreeNode oldParent = dragNode.Parent; // Remove Original Node, set the dragged node as selected dragNode.Remove(); this.SelectedNode = newTreeNode; this.Cursor = Cursors.Default; // Call drag complete event if ( this.DragComplete != null ) { DragCompleteEventArgs ea = new DragCompleteEventArgs(); ea.RemovedNode = dragNode; ea.NewParent = targetNode; ea.OldParent = oldParent; ea.CloneNode = newTreeNode; ea.DraggedOn = draggedOn; ea.InsertIndex = insertIndex; this.DragComplete(this,ea); } } } }