Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drag and Drop between ListView and TreeView.?!

70 views
Skip to first unread message

Kam

unread,
Sep 9, 2003, 9:35:59 AM9/9/03
to
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();
}
}

}
//---------------------------------------------------

Dmitriy Lapshin [C# / .NET MVP]

unread,
Sep 9, 2003, 10:05:39 AM9/9/03
to
So what's the exact problem with your code? It looks pretty 'healthy' at the
first glance...

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kam" <bas...@hotmail.com> wrote in message
news:53bfceca.03090...@posting.google.com...

Kam

unread,
Sep 9, 2003, 12:57:39 PM9/9/03
to
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

Dmitriy Lapshin [C# / .NET MVP]

unread,
Sep 10, 2003, 3:05:19 AM9/10/03
to
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?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Kam" <bas...@hotmail.com> wrote in message
news:53bfceca.03090...@posting.google.com...

0 new messages