我正在尝试清除 stringgrid,但我收到一条不一致的访问冲突消息,该消息似乎出现在最后一列被清除之后。这是代码:
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:\Users\Owner\V0.66\Project1.exe 出现错误消息:“
0x00401c51 处的访问冲突:写入地址 0x00316572”。进程停止。使用 Step 或 Run
继续。
当我将代码更改为此错误时,该错误消失了:
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;