I didnโt work very much with TListView (I mainly use database grids), I took your question as a chance to learn something. The result of this is the following code, it is more visually focused on David's answer. It has some limitations: it will only move the first selected item, and while it moves the item, the display for vsIcon and vsSmallIcon is strange after moving.
procedure TForm1.btnDownClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index<ListView1.Items.Count then begin temp := ListView1.Items.Insert(Index+2); temp.Assign(ListView1.Items.Item[Index]); ListView1.Items.Delete(Index); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end; procedure TForm1.btnUpClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index>0 then begin temp := ListView1.Items.Insert(Index-1); temp.Assign(ListView1.Items.Item[Index+1]); ListView1.Items.Delete(Index+1); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end;
source share