Author: Tomas Rutkauskas
How can I swap two items (and their subitems) in a TListview? Is there a command ?
Answer:
Exchange two items in a TListView:
1 procedure TForm1.Button2Click(Sender: TObject);
2 var3 temp: TListItem;
4 i1, i2: Integer;
5 begin6 i1 := 0;
7 i2 := 0;
8 { pick two items to exchange at random }9 while i1 = i2 do10 begin11 i1 := Random(listview1.items.count);
12 i2 := Random(listview1.items.count);
13 end;
14 { exchange them, need to create a temp item for this }15 temp := TListitem.create(listview1.items);
16 try17 temp.Assign(listview1.items[i1]);
18 listview1.items[i1].Assign(listview1.items[i2]);
19 listview1.items[i2].Assign(temp);
20 finally21 temp.free
22 end;
23 end;