⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 016.htm

📁 delphi教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<p>ChangeGrid;</p><p>RecFileForm.Caption := FormCaption+' -- '+FileName;</p><p>NewButton.Enabled := False;</p><p>OpenButton.Enabled := False;</p><p>CloseButton.Enabled := True;</p><p>end;</p><font face="宋体" lang="ZH-CN"><p>  首先系统试图用</font>Reset<fontface="宋体" lang="ZH-CN">打开一个文件,并置</font>FileOpened<font face="宋体"lang="ZH-CN">为</font>True<font face="宋体" lang="ZH-CN">。如果文件不能打开,则引发一个</font>I/O<fontface="宋体" lang="ZH-CN">异常。在异常处理过程中,首先检测文件是否存在。若不存在则创建这个文件。否则是其它原因引发的异常,则把</font>FileOpend<fontface="宋体" lang="ZH-CN">重置为</font>False<font face="宋体" lang="ZH-CN">,</font><font face="宋体" lang="ZH-CN">并显示信息“文件不能打开”。在文件创建过程中仍可能引发异常,因而在一个嵌套的异常处理中把</font>FileOpened<fontface="宋体" lang="ZH-CN">重置为</font>False<font face="宋体" lang="ZH-CN">,并提示信息“文件不能创建”。</p><p>  有关异常处理的内容请读者参看第十二章。这段程序说明:异常处理机制不仅能使我们的程序更健壮,而且为编程提供了灵活性。</p><p>  当用户按下“创建”按钮时,系统首先弹出一个标准输入框,要求用户输入文件名,确认后系统首先检测文件是否存在。若存在则直接打开,否则创建一个新文件。打开或创建过程导致异常,则重置</font>FileName<fontface="宋体" lang="ZH-CN">和</font>FileOpened<font face="宋体" lang="ZH-CN">两个全局变量。 </p></font><p>procedure TRecFileForm.NewButtonClick(Sender: TObject);</p><p>begin</p><p>FileName := InputBox('<font face="宋体" lang="ZH-CN">输入框</font>','<fontface="宋体" lang="ZH-CN">请输入文件名</font>','');</p><p>if FileName = '' then Exit;</p><p>try</p><p>AssignFile(MethodFile,FileName);</p><p>if FileExists(FileName) then</p><p>begin</p><p>Reset(MethodFile);</p><p>Count := FileSize(MethodFile);</p><p>if Count&gt;0 then</p><p>ChangeGrid;</p><p>end</p><p>else</p><p>begin</p><p>Rewrite(MethodFile);</p><p>count := 0;</p><p>end;</p><p>FileOpened := true;</p><p>Except</p><p>on EInOutError do</p><p>begin</p><p>FileName := '';</p><p>FileOpened := False;</p><p>end;</p><p>end;</p><p>if FileOpened then</p><p>begin</p><p>NewButton.Enabled := False;</p><p>OpenButton.Enabled := False;</p><p>CloseButton.Enabled := True;</p><p>RecFileForm.Caption := FormCaption+' -- '+FileName;</p><p>end;</p><p>end;</p><font face="宋体" lang="ZH-CN"><p>  当文件打开或创建后,所要做的工作有:</p><p>  ●</font> <font face="宋体" lang="ZH-CN">若文件非空,则计算文件长度,并用文件内容填充</font>StringGrid1</p><font face="宋体" lang="ZH-CN"><p>  ●</font> <font face="宋体" lang="ZH-CN">“创建”、“打开”按钮变灰,“关闭”按钮使能</p><p>  ●</font> <font face="宋体" lang="ZH-CN">把文件名附到窗口标题后</p></font><p>6.2.4 <font face="宋体" lang="ZH-CN">记录文件的读入和显示 </p><p>  定义一个全局变量</font>Count<font face="宋体" lang="ZH-CN">用来保存文件中的记录个数。当文件装入时: </p><p>  </font>Count := FileSize(MethodFile)<font face="宋体" lang="ZH-CN">; </p><p>  如果</font>Count &gt; 0<font face="宋体" lang="ZH-CN">,则首先确定</font>StringGrid1<fontface="宋体" lang="ZH-CN">的高度、行数。为保证</font>StringGrid1<fontface="宋体" lang="ZH-CN">不会覆盖窗口下面的编辑框,定义一个常量</font>MaxShow<fontface="宋体" lang="ZH-CN">。当</font>Count &lt; MaxShow<font face="宋体" lang="ZH-CN">时,记录可全部显示;当</font>Count&gt;= MaxShow<font face="宋体" lang="ZH-CN">时,</font>StringGrid1<font face="宋体"lang="ZH-CN">自动添加一个滚动棒。为保证滚动棒不覆盖掉显示内容,</font>StringGrid1<fontface="宋体" lang="ZH-CN">的宽度应留有余地。</p><p>  确定</font>StringGrid1<font face="宋体" lang="ZH-CN">高度、行数的代码如下: </p><p>  </font>With StringGrid do</p><p>if count &lt; MaxShow then</p><p>Height := DefaultRowHeight * (Count+1)+10</p><p>else</p><p>Height := DefaultRowHeight * MaxShow+10;</p><p>RowCount := Count+1;</p><p>end; </p><p><font face="宋体" lang="ZH-CN">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 而后从文件中逐个读入记录并显示在</font>StringGrid1<fontface="宋体" lang="ZH-CN">的相应位置: </p><p>  </font>for i := 1 to Count do </p><p>begin</p><p>Read(MethodFile,MethodRec);</p><p>ShowMethod(MethodRec,i);</p><p>end; </p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ShowMehtod<font face="宋体"lang="ZH-CN">是一个过程,用来把一条记录填入</font>StringGrid1<fontface="宋体" lang="ZH-CN">的一行中。对于</font>Name<font face="宋体"lang="ZH-CN">、</font>Condition<font face="宋体" lang="ZH-CN">域而言,只须直接赋值即可;而对</font>Nature<font face="宋体" lang="ZH-CN">域需要把枚举类型值转化为对应意义的字符串</font>(0<fontface="宋体" lang="ZH-CN">:“微观”,</font>1<font face="宋体" lang="ZH-CN">:“宏观”</font>)<fontface="宋体" lang="ZH-CN">;而对</font>Result<font face="宋体" lang="ZH-CN">域则需要把数值转化为一定格式的字符串: </p></font><p>Str (MethodRec.Result:6:4,ResultStr)<font face="宋体" lang="ZH-CN">;</p><p>  </font>StringGrid1.Cells[3,Pos] := ResultStr; </p><p><font face="宋体" lang="ZH-CN">即</font>Result<font face="宋体" lang="ZH-CN">显示域宽为</font>6<fontface="宋体" lang="ZH-CN">,其中小数点后位数为</font>4<font face="宋体"lang="ZH-CN">。 </p></font><p>6.2.5 <font face="宋体" lang="ZH-CN">增加一条记录 </p><p>  当用户单击“增加”按钮时屏幕将会弹出一个记录编辑模式对话框</font>EditForm<fontface="宋体" lang="ZH-CN">。在编辑框中填入合适的内容并按</font>OK<fontface="宋体" lang="ZH-CN">键关闭后,相应值写入一个</font>TMethod<fontface="宋体" lang="ZH-CN">类型的变量</font>MethodRec<font face="宋体" lang="ZH-CN">中。其中</font>Nature<fontface="宋体" lang="ZH-CN">和</font>Result <font face="宋体" lang="ZH-CN">域需要进行转换。之后增加的记录添加到</font>StringGrid1<fontface="宋体" lang="ZH-CN">的显示中。</p><p>  最后文件定位于尾部,写入当前记录,总记录数加</font>1<fontface="宋体" lang="ZH-CN">。 </p></font><p><font face="宋体" lang="ZH-CN"> </font> Seek(MethodFile,Count);</p><p>Write(MethodFile,MethodRec);</p><p>Count := Count+1; </p><p><font face="宋体" lang="ZH-CN">完整的程序清单如下: </p></font><p>procedure TRecFileForm.AddButtonClick(Sender: TObject);</p><p>var</p><p>MethodRec: TMethod;</p><p>Rl: Real;</p><p>k: Integer;</p><p>EditForm: TEditForm;</p><p>begin</p><p>if FileOpenEd = False then Exit;</p><p>EditForm := TEditForm.Create(self);</p><p>if EditForm.ShowModal &lt;&gt; idCancel then</p><p>begin</p><p>HazAttr.text := '';</p><p>MethodRec.Name := EditForm.MethodName.text;</p><p>MethodRec.Condition := EditForm.Condition.text;</p><p>case EditForm.NatureCombo.ItemIndex of</p><p>0:</p><p>MethodRec.Nature := Micro;</p><p>1:</p><p>MethodRec.Nature := Macro ;</p><p>end;</p><p>Val(EditForm.Result.text,Rl,k);</p><p>MethodRec.Result := Rl;</p><p>with StringGrid1 do</p><p>begin</p><p>if Count &lt; MaxShow then</p><p>Height := Height+DefaultRowHeight;</p><p>RowCount := RowCount+1;</p><p>end;</p><p>ShowMethod(MethodRec,Count+1);</p><p>seek(MethodFile,Count);</p><p>write(MethodFile,MethodRec);</p><p>Count := Count+1;</p><p>end;</p><p>end; </p><p>6.2.6 <font face="宋体" lang="ZH-CN">修改记录 </p><p>  首先获取当前记录位置: </p><p>  </font>CurrentRec := StringGrid1.Row - 1; </p><p><font face="宋体" lang="ZH-CN">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 而后打开编辑对话框并显示当前值。修改完毕后,修改结果保存在一个记录中并在</font>StringGrid1<fontface="宋体" lang="ZH-CN">中重新显示。</p><p>  最后修改结果写入文件: </p></font><p>Seek(MethodFile,CurrentRec);</p><p>Write(MethodFile,MethodRec); </p><p><font face="宋体" lang="ZH-CN">完整程序如下: </p></font><p>procedure TRecFileForm.ModifyButtonClick(Sender: TObject);</p><p>var</p><p>MethodRec: TMethod;</p><p>Rl: Real;</p><p>k: Integer;</p><p>EditForm: TEditForm;</p><p>begin</p><p>if FileOpened = False then Exit;</p><p>EditForm := TEditForm.Create(self);</p><p>CurrentRec := StringGrid1.Row-1;</p><p>with EditForm do</p><p>begin</p><p>MethodName.text := StringGrid1.Cells[0,CurrentRec+1];</p><p>Condition.text := StringGrid1.Cells[1,CurrentRec+1];</p><p>if StringGrid1.Cells[2,CurrentRec+1] = '<font face="宋体" lang="ZH-CN">微</font> <fontface="宋体" lang="ZH-CN">观</font>' then</p><p>NatureCombo.ItemIndex := 0</p><p>else</p><p>NatureCombo.ItemIndex := 1;</p><p>Result.text := StringGrid1.Cells[3,CurrentRec+1];</p><p>if ShowModal &lt;&gt; idCancel then</p><p>begin</p><p>HazAttr.text := '';</p><p>MethodRec.Name := MethodName.text;</p><p>MethodRec.Condition := Condition.text;</p><p>case NatureCombo.ItemIndex of</p><p>0:</p><p>MethodRec.Nature := Micro;</p><p>1:</p><p>MethodRec.Nature := Macro ;</p><p>end;</p><p>Val(Result.text,Rl,k);</p><p>MethodRec.Result := Rl;</p><p>ShowMethod(MethodRec,CurrentRec+1);</p><p>seek(MethodFile,CurrentRec);</p><p>write(MethodFile,MethodRec);</p><p>end;</p><p>end;</p><p>end;<font face="宋体" lang="ZH-CN">  </font></p><hr  width="94%"></TD><TD CLASS="tt3" VALIGN="bottom" width="8%"  ><strong><A HREF="017.htm"><FONT style="FONT-SIZE: 9pt">后一页</font></A><BR><A HREF="015.htm"><FONT style="FONT-SIZE: 9pt">前一页</font></A><BR><A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目录</font></A><BR></strong></TD></TR></table></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -