我試圖清除一個stringgrid,但我得到一個不一致的訪問衝突消息,似乎在最後一列已被清除後出現。下面是代碼:清理stringgrid時的EAcessViolation
procedure ClearTable;
var
i:integer;
begin
for i := 0 to 3 do
begin
frmHighscores.HighscoreTable.Cols[i].Clear;
end;
end;
,這裏是調用它的程序:
procedure TfrmHighscores.sortbtnClick(Sender: TObject);
var
SortedScores :array of Thighscore;
i: integer;
Ascending:boolean;
begin
ClearTable;
Case sortRGP.ItemIndex of
0: Ascending := False;
1: Ascending :=True;
end;
AssignFile(HighScoreFile, 'HighScoreFile.DAT');
Reset(HighScoreFile);
If Filesize(Highscorefile) <= 1 then
begin
showmessage('There arent enough items to sort!');
end;
If Filesize(Highscorefile) > 1 then
begin
SetLength(SortedScores, Filesize(Highscorefile)-1);
i:=0;
While not eof(HighScoreFile) do
begin
Read(Highscorefile, Highscore[i+1]);
sortedScores[i].Name := Highscore[i+1].Name;
sortedScores[i].Score := Highscore[i+1].Score;
sortedScores[i].DateSet := Highscore[i+1].DateSet;
sortedScores[i].Difficulty := Highscore[i+1].Difficulty;
inc(i);
end;
Closefile(highscorefile);
Quicksort(SortedScores, Low(SortedScores), High(SortedScores)+1, Ascending);
end;
end;
當我嘗試運行它是
項目C錯誤消息:\用戶\所有者\ V0.66 \ Project1.exe出現錯誤消息:'訪問
違反0x00401c51:寫入地址0x00316572'。進程已停止。使用步驟或運行
繼續。
錯誤消失時,我的代碼改成這樣:
procedure ClearTable;
var
i:integer;
begin
for i := 0 to 3 do
begin
showmessage('Attempting to clear Col ' +inttostr(i));
frmHighscores.HighscoreTable.Cols[i].Clear;
showmessage('Col ' +inttostr(i) + ' cleared successfully');
end;
end;