定義全局變量 Count 保存記錄總數:Count := FileSize(MethodFile)。根據記錄數動態調整 StringGrid1 高度與行數:
Count < MaxShow:全部顯示,高度 = DefaultRowHeight * (Count+1) + 10Count >= MaxShow:啟用滾動條,高度 = DefaultRowHeight * MaxShow + 10(預留寬度防滾動條遮擋內容)With StringGrid1 do
if Count < MaxShow then
Height := DefaultRowHeight * (Count + 1) + 10
else
Height := DefaultRowHeight * MaxShow + 10;
RowCount := Count + 1;
end;
逐條讀取記錄並調用 ShowMethod 顯示:
for i := 1 to Count do
begin
Read(MethodFile, MethodRec);
ShowMethod(MethodRec, i);
end;
Str(MethodRec.Result:6:4, ResultStr) 格式化為寬度 6、小數 4 位字符串彈出模態編輯對話框 EditForm,用戶確認後將數據寫入 TMethod 變量,枚舉與實數字段需轉換。隨後更新網格顯示,文件指針定位尾部寫入,記錄總數加 1。
procedure TRecFileForm.AddButtonClick(Sender: TObject);
var
MethodRec: TMethod;
Rl: Real;
k: Integer;
EditForm: TEditForm;
begin
if FileOpened = False then Exit;
EditForm := TEditForm.Create(Self);
if EditForm.ShowModal <> idCancel then
begin
HazAttr.Text := '';
MethodRec.Name := EditForm.MethodName.Text;
MethodRec.Condition := EditForm.Condition.Text;
case EditForm.NatureCombo.ItemIndex of
0: MethodRec.Nature := Micro;
1: MethodRec.Nature := Macro;
end;
Val(EditForm.Result.Text, Rl, k);
MethodRec.Result := Rl;
with StringGrid1 do
begin
if Count < MaxShow then Height := Height + DefaultRowHeight;
RowCount := RowCount + 1;
end;
ShowMethod(MethodRec, Count + 1);
Seek(MethodFile, Count);
Write(MethodFile, MethodRec);
Count := Count + 1;
end;
end;
獲取當前行號(CurrentRec := StringGrid1.Row - 1),打開編輯對話框並回填當前值。修改確認後更新網格顯示,文件定位至 CurrentRec 覆寫記錄。
procedure TRecFileForm.ModifyButtonClick(Sender: TObject);
var
MethodRec: TMethod;
Rl: Real;
k: Integer;
EditForm: TEditForm;
begin
if FileOpened = False then Exit;
EditForm := TEditForm.Create(Self);
CurrentRec := StringGrid1.Row - 1;
with EditForm do
begin
MethodName.Text := StringGrid1.Cells[0, CurrentRec + 1];
Condition.Text := StringGrid1.Cells[1, CurrentRec + 1];
if StringGrid1.Cells[2, CurrentRec + 1] = '微觀' then
NatureCombo.ItemIndex := 0
else
NatureCombo.ItemIndex := 1;
Result.Text := StringGrid1.Cells[3, CurrentRec + 1];
if ShowModal <> idCancel then
begin
HazAttr.Text := '';
MethodRec.Name := MethodName.Text;
MethodRec.Condition := Condition.Text;
case NatureCombo.ItemIndex of
0: MethodRec.Nature := Micro;
1: MethodRec.Nature := Macro;
end;
Val(Result.Text, Rl, k);
MethodRec.Result := Rl;
ShowMethod(MethodRec, CurrentRec + 1);
Seek(MethodFile, CurrentRec);
Write(MethodFile, MethodRec);
end;
end;
end;