ObjectListView and Drag & Drop - ObjectListView v2.6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

28/11/2013

ObjectListView and Drag & Drop ObjectListView v2.6.0 documentation

ObjectListView and Drag & Drop


Dropping the drag from Drag & Drop
As of v2.2, ObjectListView has sophisticated support for dragging and dropping. This support makes it easy to support interapplication drag and drop, dragging within your application, and making lists that can be rearranged by dragging.

Home Whats New? Features

The easy way


The simpliest way to use drag and drop in an ObjectListView is through the IsSimpleDragSource and IsSimpleDropSink properties (which can be set through the IDE). Setting these gives an ObjectListView that will allow objects to be dragged and dropped on items, like this:

Getting Started Cookbook FAQ Blog Download


Version 2.6.0 (2012/10/20)

Discussion Samples Search

If you set IsSimpleDragSource to true, the ObjectListView will be able to initiate drags. It will drag the currently selected items, as well as creating text and HTML versions of those rows that can be dropped onto other programs. If you set IsSimpleDropSink to true, the ObjectListView will be able to receive drops. The normal drop sink does a lot of work for you: figuring out which item is under the mouse, handling auto scrolling, drawing user feedback. However, there are two things that it cant figure out for itself: 1. Are the dragged objects allowed to be dropped at the current point? 2. What should happen when the drop occurs? So, the normal drop sink triggers two events: CanDrop and Dropped. To actually be useful, you need to handle these events. You can set up handlers for these events within the IDE, like normal. You can alternatively listen for the ModelCanDrop and ModelDropped events. This second pair of events are triggered when the source of the drag is another ObjectListView. These events work the same as the CanDrop and Dropped events except that the argument block includes useful information: The ObjectListView initiated the drag The model objects are being dragged The model object is current target of the drop

http://objectlistview.sourceforge.net/cs/dragdrop.html#dragdrop-label

1/5

28/11/2013

ObjectListView and Drag & Drop ObjectListView v2.6.0 documentation

Handling the events - CanDrop


In the CanDrop (or ModelCanDrop) event, the handler has to decide if the currently dragged items can be dropped at the current location. To indicate what operation is allowed, the handler must set the Effect property:
p r i v a t ev o i dl i s t V i e w S i m p l e _ M o d e l C a n D r o p ( o b j e c ts e n d e r ,M o d e l D r o p E v e n t A r g se ){ P e r s o np e r s o n=e . T a r g e t M o d e la sP e r s o n ; i f( p e r s o n= =n u l l ){ e . E f f e c t=D r a g D r o p E f f e c t s . N o n e ; }e l s e{ i f( p e r s o n . M a r i t a l S t a t u s= =M a r i t a l S t a t u s . M a r r i e d ){ e . E f f e c t=D r a g D r o p E f f e c t s . N o n e ; e . I n f o M e s s a g e=" C a n ' td r o po ns o m e o n ew h oi sa l r e a d ym a r r i e d " ; }e l s e{ e . E f f e c t=D r a g D r o p E f f e c t s . M o v e ; } } }

Inside the CanDrop handler, you can set the InfoMessage property to a string. If you do this, the string will be shown to the user while they are dragging. This can be used to tell the user why something cannot be dropped at that particular point, or to explain what will happen if the drop occured there:

This message is displayed by a specialised TextOverlay, which is exposed through the Billboard property of the SimpleDropSink class. You can make changes to the messages appearance through this property.

Handling the events - Dropped


If the allowed effect was anything other than None, then when the items are dropped, a Dropped (or a ModelDropped) event will be triggered. This is where the actual work of processing the dropped item should occur. A silly example from the demo looks like this:
p r i v a t ev o i dl i s t V i e w S i m p l e _ M o d e l D r o p p e d ( o b j e c ts e n d e r ,M o d e l D r o p E v e n t A r g se ){ / /I ft h e yd i d n ' td r o po na n y t h i n g ,t h e nd o n ' td oa n y t h i n g i f( e . T a r g e t M o d e l= =n u l l ) r e t u r n ; / /C h a n g et h ed r o p p e dp e o p l ep l u st h et a r g e tp e r s o nt ob em a r r i e d ( ( P e r s o n ) e . T a r g e t M o d e l ) . M a r i t a l S t a t u s=M a r i t a l S t a t u s . M a r r i e d ; f o r e a c h( P e r s o npi ne . S o u r c e M o d e l s ) p . M a r i t a l S t a t u s=M a r i t a l S t a t u s . M a r r i e d ; / /F o r c et h e mt or e f r e s h e . R e f r e s h O b j e c t s ( ) ; }

The ModelDropped event has a convenience method, RefreshObjects(), which refreshes all the objects involved in the
http://objectlistview.sourceforge.net/cs/dragdrop.html#dragdrop-label 2/5

28/11/2013

ObjectListView and Drag & Drop ObjectListView v2.6.0 documentation

operation. This is particularly useful with operations on TreeListViews.

Simply doing more


If you want to do more than this, you have to start playing with the objects that actually implement the drag and drop: SimpleDataSource and SimpleDropSink (though calling the latter simple is a bit of a misnomer).

SimpleDataSource
The major task of the SimpleDataSource is to setup a DataObject which can be used for dragging and dropping. If you want your ObjectListView to support other data formats, you will need subclass SimpleDataSource and add the data formats you want.

SimpleDropSink
SimpleDropSink is a lot more interesting and a lot more configurable. It can be made to accept drops between items:
m y D r o p S i n k . C a n D r o p B e t w e e n=t r u e ;

Or drops on the background:


m y D r o p S i n k . C a n D r o p b a c k g r o u n d=t r u e ;

Or drops on individual sub-items:


m y D r o p S i n k . C a n D r o p O n S u b I t e m s=t r u e ;

http://objectlistview.sourceforge.net/cs/dragdrop.html#dragdrop-label

3/5

28/11/2013

ObjectListView and Drag & Drop ObjectListView v2.6.0 documentation

It can also change the color used for the drag drop feedback:
m y D r o p S i n k . F e e d b a c k C o l o r=C o l o r . I n d i a n R e d ;

Doing a lot more - Drag and Drop the hard way


Its not really that hard just more work than the easy way. If you want to have complete control of the dragging process, you can implement the IDragSource interface, and then give that implementation to the ObjectListView by setting the DragSource property. Similarly, if you want to have complete control of the dropping process, you can implement the IDropSink interface, and then give that implementation to the ObjectListView by setting the DropSink property. For maximum flexibility, the IDropSink basically just unifies the full suite of Windows drag-drop messages:
p u b l i ci n t e r f a c eI D r o p S i n k { O b j e c t L i s t V i e wL i s t V i e w{g e t ;s e t ;} v o i dD r a w F e e d b a c k ( G r a p h i c sg ,R e c t a n g l eb o u n d s ) ; v o i dD r o p ( D r a g E v e n t A r g sa r g s ) ; v o i dE n t e r ( D r a g E v e n t A r g sa r g s ) ; v o i dG i v e F e e d b a c k ( G i v e F e e d b a c k E v e n t A r g sa r g s ) ; v o i dL e a v e ( ) ; v o i dO v e r ( D r a g E v e n t A r g sa r g s ) ; v o i dQ u e r y C o n t i n u e ( Q u e r y C o n t i n u e D r a g E v e n t A r g sa r g s ) ; }

The only new method in this list is the DrawFeedback() method. This is where the DropSink can draw feedback onto the ObjectListView to indicate the state of the drop. This drawing is done over the top of the ObjectListView and this will normally involve some form of alpha blending. In almost all cases, you can subclass AbstractDropSink which provides minimal implementations of all these methods.

http://objectlistview.sourceforge.net/cs/dragdrop.html#dragdrop-label

4/5

28/11/2013

ObjectListView and Drag & Drop ObjectListView v2.6.0 documentation

Rearranging rows by dragging


One common use for drag and drop is to provide a rearrangeable ObjectListView. This is so common that there is a prebuild component to do this for you. This is done by installing a RearrangingDropSink:
t h i s . o b j e c t L i s t V i e w 1 . D r a g S o u r c e=n e wS i m p l e D r a g S o u r c e ( ) ; t h i s . o b j e c t L i s t V i e w 1 . D r o p S i n k=n e wR e a r r a n g i n g D r o p S i n k ( f a l s e ) ;

This turns objectListView1 into a rearrangeble list, where the user can rearrange the rows by dragging them. The false parameter says that this sink will not accept drags from other ObjectListViews. The class is clever but it is not magical. It works even when the ObjectListView is sorted or grouped, but it is up to the programmer to decide what rearranging such lists means. Example: if the control is grouping students by academic grade, and the user drags a Fail grade student into the A+ group, it is the responsibility of the programmer to makes the appropriate changes to the model and redraw/rebuild the control so that the users action makes sense. Similarly, it is up to the programmer to decide what should happen if the user rearranges rows when the list is sorted. It also cannot work on DataListView, VirtualObjectListView and TreeListViews since the data in those control is outside the control of the ObjectListView. For those controls, you will have to use (or subclass) a SimpleDropSink and do the actual rearranging and refreshing yourself. See this blog for a detailed discussion of how to make a rearrangeable TreeListView.

Copyright 2006-2012, Phillip Piper. Last updated on Oct 30, 2012. Created using Sphinx 1.0.7.

http://objectlistview.sourceforge.net/cs/dragdrop.html#dragdrop-label

5/5

You might also like