How to move a lis item in a list view

well. I believe the title says it all.
I've added to buttons Up and Down, which I'm trying to let the user organize the items displayed in the list view.

Though I tried and tried I can't make it work. What can I do?

I would like source code please.

Thanks.
pin_plunderAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kretzschmarConnect With a Mentor Commented:
hi pin plunder,

for a listview:

procedure MoveItem(List : TListView; Index1, Index2 : Integer; DoSelect : Boolean);
var TmpItem : TListItem;
begin
  If (Index1 < List.Items.Count) and
     (Index2 < List.Items.Count) and
     (Index1 > -1) and
     (Index2 > -1) and
     (Index1 < Index2) then
  begin
    TmpItem := List.Items.Insert(Index1);
    TmpItem.Assign(List.Items[Index2+1]);
    List.Items[Index2+1].Delete;
    If DoSelect then List.Selected := TmpItem;
  end;
end;

//Move Down
procedure TForm1.Button1Click(Sender: TObject);
begin
  MoveItem(ListView1,ListView1.Selected.Index,ListView1.Selected.Index + 1,False);
end;

//Move up
procedure TForm1.Button2Click(Sender: TObject);
begin
  MoveItem(ListView1,ListView1.Selected.Index-1,ListView1.Selected.Index,True );
end;

meikl
0
 
DonBartholomewCommented:
You will have to do it 'manually'. It does not work if you don't set the focus ont the item after it is moved.


procedure TForm1.buttonUpClick(Sender: TObject);
var x: integer;
    s1, s2: string;
begin
  x := lstbox.ItemIndex;
  if x >= 1 then
    begin
      s1 := lstbox.Items.Strings[x];
      s2 := lstbox.Items.Strings[x-1];
      lstbox.Items.Strings[x] := s2;
      lstbox.Items.Strings[x-1] := s1;
      lstbox.ItemIndex := x-1;
    end;
end;
0
 
pin_plunderAuthor Commented:
thanks for your help
0
All Courses

From novice to tech pro — start learning today.