2014-06-17 51 views
1

我在表單中有一個StringGrid,當我按Button1時,我移動了此網格中的一些單元格。這裏有一個例子:在StringGrid中撤消動作

enter image description here

當我按下Button1,我從情況的去情況B.但我希望能夠做相反的事情太多:我的意思是我當我按下名爲Button2的另一個按鈕時,喜歡從B到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); 
+0

您希望用戶撤消多少次?這通常通過在發生的每一個小變化的背景中保存列表來完成。沒有看到你的代碼,我們不可能知道如何爲你提供更多的建議。 –

+0

當你按下按鈕1時,你顯然必須跟蹤你所採取的任何動作,並將其撤消以撤銷。不可能告訴你該怎麼做,因爲你沒有向我們展示處理移動的代碼。你如何「從情況A走向情境B」? –

+0

我只需要1次撤消。我正在考慮保存StringGrid A,然後更改單元格位置(StringGrid B)。然後,如果用戶想要回到A,加載保存的StringGird(這是A)。 –

回答

4

您可以使用列數屬性來保存和恢復列的內容:

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應該在某個時候被釋放。