Hey everybody, I was hoping to get a little bit of information. I've implemented drag and drop between the listboxes and treeview, but I'm having a tough time getting it done within the treeview itself. It will show the effect of moving, but it will not delete itself from the previous node, and implement itself in the new node.
I know it's something incredibly easy, but my brain is tired!
Here's the DragDrop code:
private void tree_procAssign_DragDrop(object sender, DragEventArgs e)
{
TreeView tree = (TreeView)sender;
Point pt = new Point(e.X, e.Y);
pt = tree.PointToClient(pt);
TreeNode nodeTodropIn = this.tree_procAssign.GetNodeAt(pt);
TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
if (nodeTodropIn == null) { return; }
object data = e.Data.GetData(typeof(string));
if (data == null) { return; }
TreeNode tempNode = new TreeNode(data.ToString());
if (lbx_processorAssign.Items.Contains(data.ToString()))
{
if (nodeTodropIn.Level == 0)
{
nodeTodropIn.Nodes.Add(tempNode);
nodeTodropIn.Expand();
if (nodeTodropIn.Nodes.Contains(tempNode))
lbx_processorAssign.Items.Remove(data.ToString());
}
}
if (lbx_alphaAssign.Items.Contains(data.ToString()))
{
if (nodeTodropIn.Level == 1)
{
nodeTodropIn.Nodes.Add(tempNode);
nodeTodropIn.Expand();
if (nodeTodropIn.Nodes.Contains(tempNode))
lbx_alphaAssign.Items.Remove(data.ToString());
}
}
if (tree_procAssign.Nodes.Contains((TreeNode)e.Data.GetData(typeof(TreeNode))))
{
nodeTodropIn.Nodes.Add((TreeNode)e.Data.GetData(typeof(TreeNode)));
nodeTodropIn.Expand();
}
}
Any thoughts, or is anyone able to push me in the right direction?
Thanks!
Scott
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 21, 2009, 10:26 AM EDT
// The ItemDrag event is called when the item drag begins. Here is // where you can perform any tracking, or validate if the drag // operation should occur, and so on. private void myTree_ItemDrag(object sender, ItemDragEventArgs e) { sourceNode = (TreeNode) e.Item; DoDragDrop(e.Item.ToString(), DragDropEffects.Move | DragDropEffects.Copy); } // Define the event that occurs while the dragging happens private void myTree_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } } // Determine what node in the tree we are dropping on to (target), // copy the drag source (sourceNode), make the new node and delete // the old one. private void myTree_DragDrop(object sender, DragEventArgs e) { Point pos = myTree.PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = myTree.GetNodeAt(pos); TreeNode nodeCopy; if (targetNode != null) { nodeCopy = new TreeNode(sourceNode.Text, sourceNode.ImageIndex, sourceNode.SelectedImageIndex); if (sourceNode.Index > targetNode.Index) targetNode.Parent.Nodes.Insert(targetNode.Index, nodeCopy); else targetNode.Parent.Nodes.Insert(targetNode.Index + 1, nodeCopy); sourceNode.Remove(); myTree.Invalidate(); } }and here is a sample source code:
http://www.codeproject.com/KB/tree/treedragndropdemo.aspx
Stephen York2 reputationPosted Sep 27, 2009, 4:16 PM EDT
I've got drag and drop going, but I'd like to give more visual feedback to my users.
Basically I need to be able to drag items to anywhere in the tree view. To insert between two items I could always just highlight one of them, the ask if the insert is to be before, after or overwriting the item, but I'd like to actually draw a horizontal line between the items when the mouse hovers between them.
Anyone know how to do this?
Thanks,
Steve