Tools Used: Visual C# .NET
Drag and Drop in C# has been a question on the UseNet and many websites on C# so I have decided to tackle the problem here. This is an update of the directory tree component download on this web site. The directory component allows you to peruse directories and files. To demonstrate drag and drop, I've added to the component the capability of dragging and dropping files from within the treeview. If you hold the control key down, you can also copy files. All the source for this is in the download above.
Drag and Drop
Microsoft has added a series of properties and events to help you use drag and drop with your controls. You must set the AllowDrop property to allow for dragging and dropping within the tree view. Also there are about 5 major events available for trapping drag drop operations. We use 4 of them here: ItemDrag, DragOver, DragEnter, and DragDrop.
|Event ||How It is Triggered|
|ItemDrag ||The Initial Event triggered when the user starts to drag an item in the treeview. |
|DragOver ||This is specific to treeviews and listviews and traps the item being dragged in the event parameter. AlsoHere is where you call DoDragDrop |
|DragEnter ||This event occurs when the user drags over a drag and drop control with the mouse during a drag drop operation |
|DragLeave ||Occurs when the user moves the mouse onto the control when dragging an object. |
|DragDrop ||Occurs when the user releases the mouse over the drop target |
|GiveFeedback ||Gives feedback about the effects and the current cursor |