Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:50:47+00:00 2026-05-29T09:50:47+00:00

I need to Drag and Drop multiple items inside my Tlistbox . The code

  • 0

I need to Drag and Drop multiple items inside my Tlistbox.
The code which I am referring to is

var 
   StartingPoint : TPoint;

implementation

...

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   ListBox1.DragMode := dmAutomatic;
end;

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer) ;
var
   DropPosition, StartPosition: Integer;
   DropPoint: TPoint;
begin
   DropPoint.X := X;
   DropPoint.Y := Y;
   with Source as TListBox do
   begin
     StartPosition := ItemAtPos(StartingPoint,True) ;
     DropPosition := ItemAtPos(DropPoint,True) ;

    Items.Move(StartPosition, DropPosition) ;
   end;
end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State:  TDragState; var Accept: Boolean) ;
begin
    Accept := Source = ListBox1;
end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer) ;
begin
    StartingPoint.X := X;
    StartingPoint.Y := Y;
end; 

from here.
It works fine but what I need to achieve is like this
enter image description here.

Why I want this is because there is certain order that corresponds to these listbox items.
So instead of just having manually select each item and drag drop it,I want to enable multiple drag drop.

Any views on how can I achieve this is appreciated.
Also can suggest the use of other components if the following is possible using the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:50:47+00:00Added an answer on May 29, 2026 at 9:50 am

    This is surprisingly tricky to do well (see my first revision of this answer for an example of how to get it wrong).

    Here’s a rather easy to understand approach that solves the problem by:

    1. Removing all selected items from the list and storing them in a temporary string list.
    2. Re-adds the items to the list starting at the target index.
    3. Re-selects each re-added item.

     

    procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
      ListBox: TListBox;
      i, TargetIndex: Integer;
      SelectedItems: TStringList;
    begin
      Assert(Source=Sender);
      ListBox := Sender as TListBox;
      TargetIndex := ListBox.ItemAtPos(Point(X, Y), False);
      if TargetIndex<>-1 then
      begin
        SelectedItems := TStringList.Create;
        try
          ListBox.Items.BeginUpdate;
          try
            for i := ListBox.Items.Count-1 downto 0 do
            begin
              if ListBox.Selected[i] then
              begin
                SelectedItems.AddObject(ListBox.Items[i], ListBox.Items.Objects[i]);
                ListBox.Items.Delete(i);
                if i<TargetIndex then
                  dec(TargetIndex);
              end;
            end;
    
            for i := SelectedItems.Count-1 downto 0 do
            begin
              ListBox.Items.InsertObject(TargetIndex, SelectedItems[i], SelectedItems.Objects[i]);
              ListBox.Selected[TargetIndex] := True;
              inc(TargetIndex);
            end;
          finally
            ListBox.Items.EndUpdate;
          end;
        finally
          SelectedItems.Free;
        end;
      end;
    end;
    

    Now, it is possible to do this with a series of calls to Move but it’s hard to get it right. Each time you make a move all the indices of the selected items change. The approach I give above is my preferred method for solving this problem. Incidentally I recently worked on exactly the same problem in the context of a tree view and it’s quite tricky there too!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jQuery data grid with items&columns drag&drop, Items sort. Is there any? I need: Dragable\Dropable
Greetings, I need to be able to drag and drop items that's contained in
I need to create a drag and drop system in swing where an image
i need to be able to drag and drop my picturebox with an image
I need to write a sophisticated website that does AJax, drag and drop, component
Folks, I need a solution that allows drag-and-drop cell over the table. The cells
How can we create a drag and drop widget in word press? I need
I need an applet for drag drop upload, and have found quite a many
I need to design an interface, where user can drag drop elements into page
I'm new to jquery development. I need to get drag and drop support for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.