Author: Jonas Bilinkevicius
How do I implement drag and drop between two list boxes on a form?
Answer:
Dragging items from one list box and dropping them into another is, with Delphi,
amazingly easy to implement. I'm going to show you a couple of very simple routines
to handle two types of dragging and dropping. The first involves moving items from
one list into another, and the second involves dragging items within the same list
box.
Dragging Between Two List Boxes
To perform dragging between two list boxes, you first have to make the items in the
source list box "drag-able." The easiest way to do this is to set the DragMode
property of the list box to dmAutomatic. (I realize there are those of you who
prefer to have more precise control over dragging operations by programmatically
starting and ending drag operations with BeginDrag and EndDrag. But since this
discussion is mainly geared towards the novice, I'm going to skip that topic in
favor of getting the user up and running right away).
The second thing to do is to set the MultiSelect property to True. This is purely
optional, but it's quite useful for transferring multiple items between lists.
The next thing you have to do is let the target list accept items being dropped.
The way you typically do this is with the target list's OnDragOver event. Here's a
simple example:
1
2 procedure TMainForm.lstStudyTracersDragOver(Sender, Source: TObject; X,
3 Y: Integer; State: TDragState; var Accept: Boolean);
4 begin
5 {Only let another TListBox drop items}
6 if Source is TListBox then
7 Accept := True
8 else
9 Accept := False;
10 end;
The conditional above checks to see if the source is a list box. If it is, the
target sets its Accept property to true. The method that controls dragging and
dropping is the target list box's OnDragDrop method. But instead of writing code in
the method to handle the dropped items directly, I use a generalize method instead:
11
12 procedure TransferItemsNoDups(Sender, Source: TObject);
13 var
14 I, N: Integer;
15 Found: Boolean;
16 begin
17 with (Source as TListBox) do
18 begin
19 for I := 0 to Items.Count - 1 do
20 if Selected[I] then
21 begin
22 Found := False;
23 for N := 0 to (Sender as TListBox).Items.Count - 1 do
24 if (Sender as TListBox).Items[N] = Items[I] then
25 Found := True;
26
27 if not Found then
28 (Sender as TListBox).Items.Add(Items[I]);
29 end;
30 end;
31 end;
Notice the name of the procedure: TransferItemsNoDups. This method will iterate
through the items in the source list. For each selected item to be transferred, it
checks to see if the item exists in the target list. If it does, then the item is
skipped; otherwise, it's added to the end of the list. Since this came out of an
application I wrote, there's one bit of code I didn't insert that you might
consider doing yourself; that is, some code that will delete the selected items
once they're transferred. In that case, you'd probably write a procedure that looks
like the following:
32
33 procedure TransferItems(Sender, Source: TObject);
34 var
35 I: Integer;
36 Found: Boolean;
37 begin
38 with (Source as TListBox) do
39 begin
40 for I := 0 to Items.Count - 1 do
41 if Selected[I] then
42 (Sender as TListBox).Items.Add(Items[I]);
43
44 for I := 0 to Items.Count - 1 downto 0 do
45 if Selected[I] then
46 Items.Delete(I);
47 end;
48 end;
Notice that I didn't do any duplicate checking in the procedure above. That's
because it would be useless. This methodology is best used between two lists where
you allow your user to drag and drop between the two.
Moving Items Within a List Box
Moving items within a list box is a pretty simple thing to do. Look at the
procedure below:
49
50 procedure MoveItems(var Target: TObject; X, Y: Integer);
51 var
52 NPos: Integer;
53 begin
54 with Target as TListBox do
55 begin
56 NPos := ItemAtPos(Point(X, Y), False);
57 if (NPos >= Items.Count) then
58 Dec(NPos);
59 {Move selected item to the new position}
60 Items.Move(ItemIndex, NPos);
61 ItemIndex := NPos;
62 end;
63 end;
The first thing that happens in the code is the drop position is read into an
integer variable. Then the procedure checks the value of the position to determine
if it's in the range of the items of the list box. If not, its value is decremented
to either 0 or to the count of the items. After that, the Move method is invoked to
move the currently selected item to the new drop position, and the currently
selected item is reselected in the position it was dropped.
I didn't cover all the subtle nuances of drag and drop here. My purpose was to give you something to start with. I suggest you pore over the Delphi user manual and online help for more in-depth discussions of drag and drop functionality.
|