Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Drag and Drop for List Boxes

How do I implement drag and drop between two list boxes on a form?

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:

procedure TMainForm.lstStudyTracersDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
  {Only let another TListBox drop items}
  if Source is TListBox then
    Accept := True
        Accept := False;

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:

procedure TransferItemsNoDups(Sender, Source : TObject);
  I, N : Integer;
  Found : Boolean;
  with (Source AS TListBox) do begin
    for I := 0 to Items.Count - 1 do
      if Selected[I] then begin
        Found := False;
        for N := 0 to (Sender AS TListBox).Items.Count - 1 do
          if (Sender AS TListBox).Items[N] = Items[I] then
            Found := True;

        if NOT Found then
          (Sender AS TListBox).Items.Add(Items[I]);

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:

procedure TransferItems(Sender, Source : TObject);
  I : Integer;
  Found : Boolean;
  with (Source AS TListBox) do begin
    for I := 0 to Items.Count - 1 do
      if Selected[I] then
        (Sender AS TListBox).Items.Add(Items[I]);

        for I := 0 to Items.Count - 1 DownTo 0 do
          if Selected[I] then

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:

procedure MoveItems(var Target : TObject; X, Y : Integer);
  NPos : Integer;
  with Target AS TListBox do begin
    NPos := ItemAtPos(Point(X, Y), False);
    if (NPos >= Items.Count) then
    {Move selected item to the new position}
    Items.Move(ItemIndex, NPos);
    ItemIndex := NPos;

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.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date