[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
I decided to improve the way that items in the Parts and Supply list items could be rearranged in our Cutlist program and the logical way seemed to be the "drag and drop" method I have seen elsewhere. This is posting represents the results of my investigation into the techniques for doing that. There are three versions included in the source zip file which can be downloaded from the bottom of this page: ArrangeListbox1 uses Delphi's built-in drag/drop facility. Three event exits in Listbox1 are used with the DragType property set to dmAutomatic on a TListbox control. Not counting comments, there are just 12 lines of user written code in this simplest version.
There are two things I didn't like about the first version. I doesn't give much indication where the item would be dropped except the tip of the drag cursor arrow. Some programs, which I can't identify right now, draw a bold horizontal line where the item will be dropped. I liked that idea. Also, if the listbox contains more items than can be displayed on a single page, the only way to drag further is to drop the item at one end of the display area, scroll up or down, and continue the drag. I wanted some sort of "auto-scroll" to occur when I moved the cursor above or below the listbox border and there are un-displayed items in that direction. ArrangeListBox2 addresses the first of those features, giving visual indication of where the item would be dropped. A new TListbox OnDrawItem event exit contains the code to draw the item text and a horizontal line above or below the text (above if the text if the dragged item is below this drop point, and below if the dragged item is above the drop point). The OnDragOver exit takes over the job of forcing the redraw of the items that need to be redrawn. Here's how that works - ListBoxDragOver calls the Tlistbox ItemFromPoint method to convert the X, Y coordinates to the index of the item that is being dragged over. It then calls ListBoxDrawItem for the previous drop index to erase the bold line and then calls it again for the new drop index item to draw the bold line at the new location. The third version, ArrangeListBox3, addresses the scrolling problem. I decided that it was not feasible to solve using the standard drag operations so I replaced OnDragStart, OnDragOver, and OnDragDrop drag event exits with OnMouseDown, OnMouseMove, and OnMouseUp mouse event exits.
Addendum August 8, 2007: At a viewer's request, I added a "block move" option to the ArrangeListBox4 program (Version 4) today which allows a contiguous block of selected items to be moved in a single drag operation. There seems to be a conflict when the ExtendedSelect property is true and a left drag operation is started. So, for now, block drags are implemented as "right button" drag operations. Source code for all three versions is included in the downloadable zip file ArrangeListBoxSource.zip
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |