在Delphi的ListView中实现向上移动或者向下移动某行的功能,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
procedure TForm1.ExchangeItem(lv: TListView; const iFrom, iTo: Integer); var tempItem: TListItem; begin if iFrom = iTo then Exit; if (iTo < 0) or (iTo >= lv.Items.Count) then Exit; lv.Items.BeginUpdate; try tempItem := TListItem.Create(lv.Items); tempItem.Assign(lv.Items.Item[iFrom]); lv.Items.Item[iFrom].Assign(lv.Items.Item[iTo]); lv.Items.Item[iTo].Assign(tempItem); lv.ItemIndex := lv.Items[iTo].Index; tempItem.Free; finally lv.Items.EndUpdate end; end; procedure TForm1.btnMoveDownClick(Sender: TObject); begin ExchangeItem(lvSE, lvSE.Selected.Index, lvSE.Selected.Index + 1); end; procedure TForm1.btnMoveUpClick(Sender: TObject); begin ExchangeItem(lvSE, lvSE.Selected.Index, lvSE.Selected.Index - 1); end; |