Delphi: Dragging Object Using TListBox

For some reasons, drag and drop operation is considered to be an important construction approach in many particular applications. The objective is to make user more friendly to the application, by chunking together 2 operands into a single action. However, this operation requires more physical effort than moving the same pointing device without holding down any buttons. Because some users can't move as quickly and precisely while dragging (see Fitts' law) .

Anyway, this drag and drop operation finally used for M8 framework generator application.



The basic ideas is moving object from source to target (both TListBox objects), while object from source (TListBox A) can dragged horizontally into target (TListBox B), but the objects contained in TListBox B also able to dragged vertically.



To make objects able to drag horizontally from TListBox A into TListBox B, there should be onMouseDown trigger code in TListBox A and onTargetDragOver in TListBox B. Here's below the code:

procedure TFUtama.ListUISourceMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
dragUnder:=false;
ListUISource.BeginDrag(True);
end;

procedure TFUtama.ListUITargetDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := (Source is TListBox);
end;



To enable objects contained in TListBox B dragged vertically, use onMouseDown and onDragDrop event in TListBox B. Here's below the code:

procedure TFUtama.ListUITargetMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
StartingPoint.X := X;
StartingPoint.Y := Y;
dragUnder:=true;
end;

procedure TFUtama.ListUITargetDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
 i,j,k : Integer;
 str : string;
 DropPosition, StartPosition: Integer;
 DropPoint: TPoint;
begin
DropPoint.X := X;
DropPoint.Y := Y;
with Source as TListBox do
 begin
 for i := 0 to Items.Count - 1 do
  if (Selected[i] and not dragUnder) then (Sender AS TListBox).Items.AddObject(Items[i], Items.Objects[i])
  else if (Selected[i] and dragUnder) then
   begin
   StartPosition := ItemAtPos(StartingPoint,True) ;
   DropPosition := ItemAtPos(DropPoint,True) ;
   Items.Move(StartPosition, DropPosition) ;
     
   if StartPosition>DropPosition then // 6>1
    begin
    for j:= StartPosition-1 downto DropPosition do
     begin
     // do some operation while StartPosition > DropPosition
     end;
    end
   else
    begin
    for j:= StartPosition+1 to DropPosition do
     begin
     // do some operation while StartPosition < DropPosition
     end;
    end;
   end;
 dragUnder:=false;
 end;
end;



Conclusion:
Use this operations at your own risk! :)

Labels:

Sincerelly,

Eko Wahyudiharto
PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address:

Delivered by FeedBurner

Post a Comment Bookmark and Share

 

Post a Comment

Leave comments here...