计算机等级考试二级Delphi辅导知识:文件管理_第3页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2012年12月27日
   当文件打开或创建后,所要做的工作有:
   ● 若文件非空,则计算文件长度,并用文件内容填充StringGrid1
   ● “创建”、“打开”按钮变灰,“关闭”按钮使能
   ● 把文件名附到窗口标题后
   6.2.4 记录文件的读入和显示 
   定义一个全局变量Count用来保存文件中的记录个数。当文件装入时: 
   Count := FileSize(MethodFile); 
   如果Count > 0,则首先确定StringGrid1的高度、行数。为保证StringGrid1不会覆盖窗口下面的编辑框,定义一个常量MaxShow。当Count < MaxShow时,记录可全部显示;当Count >= MaxShow时,StringGrid1自动添加一个滚动棒。为保证滚动棒不覆盖掉显示内容,StringGrid1的宽度应留有余地。
   确定StringGrid1高度、行数的代码如下: 
   With StringGrid do
   if count < MaxShow then
   Height := DefaultRowHeight * (Count+1)+10
   else
   Height := DefaultRowHeight * MaxShow+10;
   RowCount := Count+1;
   end; 
   而后从文件中逐个读入记录并显示在StringGrid1的相应位置: 
   for i := 1 to Count do
   begin
   Read(MethodFile,MethodRec);
   ShowMethod(MethodRec,i);
   end; 
   ShowMehtod是一个过程,用来把一条记录填入StringGrid1的一行中。对于Name、Condition域而言,只须直接赋值即可;而对Nature 域需要把枚举类型值转化为对应意义的字符串(0:“微观”,1:“宏观”);而对Result域则需要把数值转化为一定格式的字符串: 
   Str (MethodRec.Result:6:4,ResultStr);
   StringGrid1.Cells[3,Pos] := ResultStr; 
   即Result显示域宽为6,其中小数点后位数为4。 
   6.2.5 增加一条记录 
   当用户单击“增加”按钮时屏幕将会弹出一个记录编辑模式对话框EditForm。在编辑框中填入合适的内容并按OK键关闭后,相应值写入一个TMethod类型的变量MethodRec中。其中Nature和Result 域需要进行转换。之后增加的记录添加到StringGrid1的显示中。
   最后文件定位于尾部,写入当前记录,总记录数加1。 
   Seek(MethodFile,Count);
   Write(MethodFile,MethodRec);
   Count := Count+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; 
   而后打开编辑对话框并显示当前值。修改完毕后,修改结果保存在一个记录中并在StringGrid1中重新显示。
   最后修改结果写入文件: 
   Seek(MethodFile,CurrentRec);
   Write(MethodFile,MethodRec); 
   完整程序如下: 
   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; 
   6.2.7 记录的删除、插入、排序 
   删除一条记录的基本思路是:获取当前记录的位置并把该位置后的记录逐个向前移动。 文件在最后一条记录前截断。 
   for i:=CurrentRec+1 to Count-1 do
   begin
   seek(MethodFile,i);
   read(MethodFile,MethodRec);
   seek(MethodFile,i-1);
   Write(MethodFile,MethodRec);
   end;
   Truncate(MethodFile); 
   为避免误删除,在进行删除操作前弹出一个消息框进行确认。删除后要更新全局变量的值和显示内容: 
   Count := Count - 1;
   ChangeGrid; 
   完整的程序如下: 
   procedure TRecFileForm.DeleteButtonClick(Sender: TObject);
   var
   NewFile: MethodFileType;
   MethodRec: TMethod;
   NewFileName: String;
   i: Integer;
   begin
   if FileOpened = False then Exit;
   CurrentRec := StringGrid1.Row-1;
   if CurrentRec < 0 then Exit;
   if MessageDlg('Delete Current Record ?', mtConfirmation,
   [mbYes, mbNo], 0) = idYes then
   begin
   HazAttr.text := '';
   for I := CurrentRec+1 to Count-1 do
   begin
   seek(MethodFile,i);
   read(MethodFile,MethodRec);
   seek(MethodFile,i-1);
   Write(MethodFile,MethodRec);
   end;
   Truncate(MethodFile);
   Count := Count-1;
   ChangeGrid;
   end;
   end;

相关文章