Welcome to AddressOf.com Sign in | Join | Help

TreeView, ListView, Drag and Drop and Command Pattern Example

Erik Porter asked me via an IM session whether doing drag and drop with a TreeView was difficult.  I explained to him that it was actually pretty straight forward, however, if you want to get the same (or at least similar) results as say if you were using Windows Explorer, you'd have to do a bit more work since that visual behavior is not part of the components that come with the Framework.

I threw together a quick example showing how to use the drag and drop (including the highlighting effect while hovering) features between the two controls, in addition I figured I'd show how to use the Command Pattern.  At this point, it's an unfinished example in the fact that it's not polished and some functions may not work as expected.  It's basically an extremely limited version of Windows Explorer style application.  For those interested, I've posted it for your browsing pleasure.  If you have any suggestions for improvement, please let me know.  (Just keep in mind that this is a very quickly thrown together sample, so please don't be too harsh ;-) )

Download TreeListViewDragDrop.zip

[update] OK, it's a bit more polished now.  Basically this example shows how to use:

  • Drag and Drop behavior between a TreeView and ListView.
  • Drag and Drop hover behavior similar to Windows Explorer.
  • In place editing within the TreeView and ListView.
  • File and Directory I/O (Move, Rename, Delete, New).
  • Command Pattern applied to menu and keyboard actions.
  • An example of a technique to show Folder / SubFolder hierarchy without trying to traverse the entire tree at one time.

[update] I should point out that the behavior being emulated is that which is common across all of the listviews I mentioned.  This example does not implement the ghosted icon behavior that some software supports.  I've looked at several methods for doing this and there is a significant complexity surrounding it.  Apparently, creating the custom mouse icon isn't too difficult, however, having it display as a ghosted icon in the same behavior is.  By this, I mean that you have to handle the feedback for every control on your form in order for the icon to stay visible.  If people are interested in the links to some of these resources I've found, let me know and I'll add them.

Published Saturday, October 2, 2004 1:29 AM by CorySmith
Filed under: ,

Comments

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, October 22, 2004 9:29 AM by Rada
it good, but have no "shadow (a picture of it but alpha maybe 50%)" of the file or folder that we drag it.

saingrada@yahoo.com

# TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, October 29, 2004 10:06 AM by dotRob

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Wednesday, April 6, 2005 4:11 AM by SAchin
tooooooo goooooooooooood

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, April 8, 2005 5:11 PM by Nike
This example is awesome...just what i was looking for. I had been searching the web for examples for 2 days now...i think this is the best so far i have found.

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Tuesday, June 14, 2005 2:33 AM by sunena
can u send me some code whereby u can drag and drop items from datagrid to listview.
any clues?

-pisces

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, June 27, 2005 10:31 AM by ajay
Not very good. wanted how can we drag with the half transperant image of the file or folder to be dragged.

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, September 2, 2005 12:55 PM by Jason
Count me among those who found this example very helpful, thank you

# Voice Record using .NET

Thursday, September 8, 2005 3:37 AM by pavankumarreddy
i want to prepare a code for voice record in my project. after finishing the voice record then click the stop button and next save button it will save in server.

kindly give the coding for this.............

thanks and regards,

pavankumarreddy

# re:ListView, Drag and Drop and Command Pattern Example

Wednesday, January 25, 2006 8:29 AM by syedshanu
I want the code for drag and drop in list view in vb.net

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, January 27, 2006 6:45 AM by upul
Examples is very good
But I'm looking for very simply how to get the selected node without AfterSelect event. I want it with Treeview1_Click(...) event. If you have any about this please send detailds.

Thanks in advance
upul

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Saturday, February 18, 2006 5:38 AM by lameri
Would you have the example in C#?

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, April 14, 2006 1:06 AM by hui
nice,can you disploy the listview control in MDI style?
please contact me: z00m64@hotmail.com

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, May 19, 2006 7:59 AM by S
Found this example very useful
Thanks

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Tuesday, June 27, 2006 12:46 PM by Brandon
Very nice example. Thank You

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, July 10, 2006 5:24 AM by Kalel
nice one!

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, August 7, 2006 5:22 AM by Rodde
you saved my life MAN! Great piece of code!

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Thursday, August 31, 2006 2:03 PM by Paul
This is a LIVE explorer example.. moving files around here moves them on your disk.
I made the mistake of thinking it was only a demo.

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, November 27, 2006 8:38 AM by Renny

How about web forms?  do you have an example of dragging dropping from listview / gridview to a treeview?

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, November 27, 2006 8:57 PM by Salemkoten

Its very good.

I want to code of drag and drop treeview to listview and drag and drop in this listview.

In vb.net 2005

Please contact me: hoa0612@gmail.com

Thanks in advance.

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Monday, December 4, 2006 3:11 PM by ehudd

I tried to convert to 2005 and it doesn't work. Wonder if someone got it to rn with the new version

Ehud

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Tuesday, January 16, 2007 9:15 PM by Bern

Allo

C'est un très belle exemple de Drag and Drop

et aussi de code.

Merci

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Tuesday, June 26, 2007 11:23 PM by cstripathi
hi this is to good but same thing is possible in vb6.0 basic or not plz help me or send me mail cst242@gmail.com

# re: TreeView, ListView, Drag and Drop and Command Pattern Example

Friday, August 31, 2007 7:02 AM by AL
Can this code be translated for Treeview to Treeview drag and drop operation? I have to create a form with two treeviews and be able to drag and drop from one treeview to the other.
Anonymous comments are disabled