Delphi: Shift-Up and Shift-Down in the Listview

后端 未结 2 567
逝去的感伤
逝去的感伤 2021-01-14 01:43

Is there a feature in the Listview control to shift items up and down?

2条回答
  •  迷失自我
    2021-01-14 02:28

    Not having worked with TListView very much (I mostly use database grids), I took your question as a chance to learn something. The following code is the result, it is more visually oriented that 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 the move.

    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 Index0 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;
    

提交回复
热议问题