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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pin_plunderAuthor Commented:
thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.