2010. március 9., kedd

Change the position of a list item in a TListView (2)


Problem/Question/Abstract:

How can I swap two items (and their subitems) in a TListview? Is there a command ?

Answer:

Exchange two items in a TListView:

procedure TForm1.Button2Click(Sender: TObject);
var
  temp: TListItem;
  i1, i2: Integer;
begin
  i1 := 0;
  i2 := 0;
  { pick two items to exchange at random }
  while i1 = i2 do
  begin
    i1 := Random(listview1.items.count);
    i2 := Random(listview1.items.count);
  end;
  { exchange them, need to create a temp item for this }
  temp := TListitem.create(listview1.items);
  try
    temp.Assign(listview1.items[i1]);
    listview1.items[i1].Assign(listview1.items[i2]);
    listview1.items[i2].Assign(temp);
  finally
    temp.free
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése