首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 記錄文件讀取顯示與記錄增刪改核心代碼詳解

     編輯:Delphi

6.2.4 記錄文件的讀入與顯示

定義全局變量 Count 保存記錄總數:Count := FileSize(MethodFile)。根據記錄數動態調整 StringGrid1 高度與行數:

  • 若 Count < MaxShow:全部顯示,高度 = DefaultRowHeight * (Count+1) + 10
  • 若 Count >= 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;

ShowMethod 字段轉換邏輯

  • Name、Condition:直接賦值
  • Nature (枚舉):0 →「微觀」,1 →「宏觀」
  • Result (實數):Str(MethodRec.Result:6:4, ResultStr) 格式化為寬度 6、小數 4 位字符串

6.2.5 增加一條記錄

彈出模態編輯對話框 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;

6.2.6 修改記錄

獲取當前行號(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;
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved