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

Delphi 記錄文件管理系統設計:任務分析、界面佈局與文件打開創建異常處理

     編輯:Delphi

6.2 記錄文件應用實例:系統安全性綜合評估方法管理系統

6.2.1 任務背景與記錄結構定義

複雜項目開發中系統安全性評估難以獲得客觀全面的值,採用多角度、多側面評估後定量集成。定義記錄類型:

type
  TNature = (Micro, Macro);  // 方法性質:微觀/宏觀
  TMethod = Record
    Name: string[20];        // 方法名
    Condition: string[40];   // 適用條件
    Nature: TNature;         // 方法性質
    Result: Real;            // 評估值
  end;

每種方法為一條記錄,每個方法集為一個記錄文件,需支持動態管理。

6.2.2 系統設計基本思路與界面佈局

核心功能:文件打開/創建/關閉/顯示、記錄增刪改、結果綜合顯示。採用兩組按鈕分離文件與記錄操作,StringGrid 顯示文件內容,唯讀編輯框 HazAttr 顯示綜合結果。

部件名稱關鍵屬性功能備註
RecFileFormBorderStyle=bsDialog
Position=poScreenCenter
文件打開後標題附加文件名
StringGrid1動態確定大小行數顯示文件記錄
HazAttrReadOnly=True顯示綜合結果
OpenButtonCaption='打開'打開記錄文件,不存在則創建
NewButtonCaption='創建'創建記錄文件,存在則打開
CloseButtonCaption='關閉'關閉已打開文件
AddButtonCaption='增加'增加一條記錄
ModifyButtonCaption='修改'修改一條記錄
DeleteButtonCaption='刪除'刪除一條記錄
CalcuButtonCaption='計算'計算並顯示最終結果
ExitButtonCaption='退出'系統終止,若有文件打開則先關閉
OpenDialog1Filter='Record File(*.Rec)|*.Rec|Any File(*.*)|*.*'選擇/輸入欲打開文件

另需編輯對話框,含四個編輯框對應 TMethod 四個字段。定義全局變量協調運行:

變量名類型作用
MethodFileMethodFileType當前打開文件變量
FileNamestring[70]當前文件名
CountInteger記錄總數
CurrentRecInteger當前處理記錄號
FileOpenedBoolean是否有文件打開,控制按鈕啟用/變灰

MethodFileType = file of TMethod。

6.2.3 記錄文件的打開與創建:異常處理機制

記錄文件打開需「關聯」與「初始化」兩步,同文本文件但無 Append。缺省讀寫方式打開,若需只讀/只寫需修改 FileMode(0=只讀、1=只寫、2=讀寫),屬全局變量,用後須還原。

「打開」按鈕邏輯

  1. 彈出標準文件打開對話框獲取文件名
  2. AssignFile 關聯,嘗試 Reset 打開
  3. 若引發 I/O 異常:檢測文件是否存在
    • 不存在 → Rewrite 創建新文件
    • 存在但無法打開 → 顯示「文件不能打開」
  4. 創建過程仍可能異常 → 捕獲顯示「文件不能創建」
  5. 成功則統計記錄數、填充網格、更新界面狀態
procedure TRecFileForm.OpenButtonClick(Sender: TObject);
begin
  if OpenDialog1.Execute then FileName := OpenDialog1.FileName else Exit;
  AssignFile(MethodFile, FileName);
  try
    Reset(MethodFile);
    FileOpened := True;
  except
    on EInOutError do
    begin
      try
        if not FileExists(FileName) then
        begin
          Rewrite(MethodFile);
          FileOpened := True;
        end
        else
        begin
          FileOpened := False;
          MessageDlg('文件不能打開', mtWarning, [mbOK], 0);
        end;
      except
        on EInOutError do
        begin
          FileOpened := False;
          MessageDlg('文件不能創建', mtWarning, [mbOK], 0);
        end;
      end;
    end;
  end;
  if not FileOpened then Exit;
  Count := FileSize(MethodFile);
  if Count > 0 then ChangeGrid;
  RecFileForm.Caption := FormCaption + ' -- ' + FileName;
  NewButton.Enabled := False;
  OpenButton.Enabled := False;
  CloseButton.Enabled := True;
end;

「創建」按鈕邏輯

彈出輸入框獲取文件名,檢測存在性:存在則 Reset 打開,不存在則 Rewrite 創建。異常時重置 FileName 與 FileOpened。

procedure TRecFileForm.NewButtonClick(Sender: TObject);
begin
  FileName := InputBox('輸入框', '請輸入文件名', '');
  if FileName = '' then Exit;
  try
    AssignFile(MethodFile, FileName);
    if FileExists(FileName) then
    begin
      Reset(MethodFile);
      Count := FileSize(MethodFile);
      if Count > 0 then ChangeGrid;
    end
    else
    begin
      Rewrite(MethodFile);
      Count := 0;
    end;
    FileOpened := True;
  except
    on EInOutError do
    begin
      FileName := '';
      FileOpened := False;
    end;
  end;
  if FileOpened then
  begin
    NewButton.Enabled := False;
    OpenButton.Enabled := False;
    CloseButton.Enabled := True;
    RecFileForm.Caption := FormCaption + ' -- ' + FileName;
  end;
end;

文件打開/創建後的通用處理

  • 非空文件:計算長度,內容填充 StringGrid1
  • 「創建」「打開」按鈕變灰,「關閉」按鈕啟用
  • 文件名附加到窗口標題

異常處理機制不僅增強健壯性,更為編程提供了靈活性(詳見第十二章)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved