撤消StringGrid中的操作

时间:2014-06-17 22:17:39

标签: delphi lazarus

我的表单中有一个StringGrid,当我按下Button1时,我会在这个网格中移动一些单元格。这是一个例子:

enter image description here

当我按下Button1时,我会从情况A转到情况B.但我也希望能够做相反的事情:我的意思是我想从B出发当我按下另一个名为Button2的按钮时,到A。

我想创建类似“撤消”按钮的东西。我怎么能这样做?我正在使用拉撒路。

我只需要做1次撤消。这是移动StringGrid单元格的过程:

procedure TForm1.SortGrid(Grid : TStringGrid; const SortCol:integer; const datatype:integer; const ascending:boolean);

var
   i : integer;
   tempgrid:tstringGrid;
   list:array of integer;
begin
  tempgrid:=TStringgrid.create(self);

  with tempgrid do
  begin
    rowcount:=grid.rowcount;
    colcount:=grid.colcount;
    fixedrows:=grid.fixedrows;
  end;

  with Grid do
  begin
    setlength(list,rowcount-fixedrows);
    for i:= fixedrows to rowcount-1 do
    begin
      list[i-fixedrows]:=i;
      tempgrid.rows[i].assign(grid.rows[i]);
    end;

    //Call the procedure and sort the stuff 
    Quicksort(Grid, list,0,rowcount-fixedrows-1,sortcol,datatype, ascending);

    for i:=0 to rowcount-fixedrows-1 do
    begin
      rows[i+fixedrows].assign(tempgrid.rows[list[i]])
    end;
    row:=fixedrows;
  end;

  tempgrid.free;
  setlength(list,0);
  screen.cursor:=crdefault;

end; 

当我点击Button1 ...

SortGrid(StringGrid1,1,1,true);

1 个答案:

答案 0 :(得分:4)

您可以使用Cols属性保存和恢复该列的内容:

var
  MySavedData: TStringList;
begin
  ...
  MySavedData := TStringList.Create;
  // Save the contents of the column 1
  MySavedData.Assign(StringGrid1.Cols[1]);
  SortGrid(StringGrid1,1,1,true);
  // Restore the contents of the column 1
  StringGrid1.Cols[1] := MySavedData;
  ...
end;

显然,MySavedData应该在某个时候被释放。

相关问题