Drag and Drop between ListView and TreeView.?!

  • Thread starter Thread starter Kam
  • Start date Start date
K

Kam

I've got a treeview and listview program which i would like to add a
drag and drop utility to it. i've managed to drag and drop items from
the treeview to the listview, from the treeview to itself. but i
cudn't do the same from the listview to the treeview.?!!

i would like to be able to drag and drop items/nodes from both to
either one of them and update the data automatically and refresh the
controls.

I don't know how to do this and i've got a very short time to do it.
Any help, examples or comments would be very much appreciated.

Thank you in advance.

Regards,
Kam

The treeview looks like:
Level1
--Level2
---Level3

and the code i've developed is as follows:
//-----------------------------------------------------


private void treeView1_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
treeView1.DoDragDrop (e.Item, DragDropEffects.Move);
}

private void listView1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent (typeof (TreeNode)))
e.Effect = DragDropEffects.Move; //All or Copy (try)
}

private void listView1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent (typeof (TreeNode)))
{
TreeNode tn = (TreeNode) e.Data.GetData
(typeof (TreeNode));
listView1.Items.Add (tn.Text, tn.ImageIndex);
treeView1.Nodes.Remove (tn);
}
}

private void listView1_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
listView1.DoDragDrop (e.Item, DragDropEffects.Move);
}

private void treeView1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void treeView1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{

TreeNode NewNode;
if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));

TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);

NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if(DestinationNode!= NewNode)
{
DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
DestinationNode.Expand();
//Remove Original Node
NewNode.Remove();
}
}

}
//---------------------------------------------------
 
So what's the exact problem with your code? It looks pretty 'healthy' at the
first glance...
 
Iam unable to drag an item from the listview to the treeview !!

i've managed to drag and drop as follows:

-treeview to itself
-treeview to listview

but i would like to be able to drag and drop from the listitem to the
treeview, update the database (which is not done in any of the above)
and refresh both controls with the new changes!

any idea? thank u and lookin forward for yr replies.

regards,
kam
 
Probably you should try using a custom class as the drag item, instead of
dragging list view items themselves in the ListView_ItemDrag handler.

By the way - do you see the mouse pointer change when you attempt dragging
an item from the ListView? Have you checked that the ItemDrag event fires?
Have you checked that the TreeView receives the DragDrop message and the
correct data object is obtained?
 
Back
Top