6.2.4 and display of record files
Define a global variable count to save the number of records in a file. When the file is loaded:
Count: = FileSize (Methodfile);
If Count > 0, first determine the height of the StringGrid1, the number of rows. To ensure that STRINGGRID1 does not overwrite the edit box below the window, define a constant maxshow. When Count < Maxshow, the records are all displayed, and when Count >= Maxshow, STRINGGRID1 automatically adds a scroll bar. In order to ensure that the scroll bar does not cover the display content, the width of the StringGrid1 should be leeway.
The code that determines the STRINGGRID1 height and number of rows is as follows:
With Stringgrid do
If Count < Maxshow Then
Height: = Defaultrowheight * (count+1) +10
Else
Height: = Defaultrowheight * MAXSHOW+10;
ROWCOUNT: = count+1;
End
Then read the records one at a time from the file and display them at the appropriate location in STRINGGRID1:
For I: = 1 to Count do
Begin
Read (METHODFILE,METHODREC);
Showmethod (Methodrec,i);
End
Showmehtod is a process used to fill a record into a StringGrid1 line. For the name, condition domain, you only have to assign the value directly, but to the nature domain needs to convert the enumeration type value to the corresponding meaning of the string (0: "Micro", 1: "Macro"), and the result field needs to convert the value to a certain format of the string:
STR (METHODREC.RESULT:6:4,RESULTSTR);
Stringgrid1.cells[3,pos]: = ResultStr;
The result displays a field width of 6, where the number of digits after the decimal point is 4.
6.2.5 Add a record
When the user clicks the Add button, the screen will pop up with a record editing mode dialog box editform. When you fill in the appropriate content in the edit box and press OK to close, the corresponding value is written to a tmethod type of variable Methodrec. Where the nature and result fields need to be converted. The added records are then added to the STRINGGRID1 display.
The final file is positioned at the tail, writes the current record, and adds 1 to the total record number.
Seek (Methodfile,count);
Write (METHODFILE,METHODREC);
Count: = count+1;
The complete list of programs is as follows:
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 Modify Records
Get the current record location first:
Currentrec: = stringgrid1.row-1;
Then open the Edit dialog box and display the current value. After the modifications are completed, the results are saved in a record and StringGrid1 in the display.
The final modification results are written to the file:
Seek (METHODFILE,CURRENTREC);
Write (METHODFILE,METHODREC);
The complete procedure is as follows:
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] = ' microscopic ' 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