📄 mainfrm.pas
字号:
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Mask, Persrec, ComCtrls;
const
// Declare the file name as a constant
FName = 'PERSONS.DAT';
type
TMainForm = class(TForm)
edtFirstName: TEdit;
edtLastName: TEdit;
edtMI: TEdit;
meAge: TMaskEdit;
lblFirstName: TLabel;
lblLastName: TLabel;
lblMI: TLabel;
lblBirthDate: TLabel;
lblAge: TLabel;
btnFirst: TButton;
btnNext: TButton;
btnPrev: TButton;
btnLast: TButton;
btnAppend: TButton;
btnUpdate: TButton;
btnClear: TButton;
lblRecNoCap: TLabel;
lblRecNo: TLabel;
lblNumRecsCap: TLabel;
lblNoRecs: TLabel;
dtpBirthDay: TDateTimePicker;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnAppendClick(Sender: TObject);
procedure btnUpdateClick(Sender: TObject);
procedure btnFirstClick(Sender: TObject);
procedure btnNextClick(Sender: TObject);
procedure btnLastClick(Sender: TObject);
procedure btnPrevClick(Sender: TObject);
procedure btnClearClick(Sender: TObject);
public
PersonRec: TPersonRec;
RecordStream: TRecordStream;
procedure ShowCurrentRecord;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.FormCreate(Sender: TObject);
begin
{ If the file does not exist, then create it, otherwise, open it for
both read and write access. This is done by instantiating
a TRecordStream }
if FileExists(FName) then
RecordStream := TRecordStream.Create(FName, fmOpenReadWrite)
else
RecordStream := TRecordStream.Create(FName, fmCreate);
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
RecordStream.Free; // Free the TRecordStream instance
end;
procedure TMainForm.ShowCurrentRecord;
begin
// Read the current record.
RecordStream.ReadRec(PersonRec);
// Copy the data from the PersonRec to the form's controls
with PersonRec do
begin
edtFirstName.Text := FirstName;
edtLastName.Text := LastName;
edtMI.Text := MI;
dtpBirthDay.Date := BirthDay;
meAge.Text := IntToStr(Age);
end;
// Show the record number and total records on the main form.
lblRecNo.Caption := IntToStr(RecordStream.CurRec);
lblNoRecs.Caption := IntToStr(RecordStream.NumRecs);
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
// Display the current record only if one exists.
if RecordStream.NumRecs <> 0 then
ShowCurrentRecord;
end;
procedure TMainForm.btnAppendClick(Sender: TObject);
begin
// Copy the contents of the form controls to the PersonRec record
with PersonRec do
begin
FirstName := edtFirstName.Text;
LastName := edtLastName.Text;
MI := edtMI.Text;
BirthDay := dtpBirthDay.Date;
Age := StrToInt(meAge.Text);
end;
// Write the new record to the stream
RecordStream.AppendRec(PersonRec);
// Display the current record.
ShowCurrentRecord;
end;
procedure TMainForm.btnUpdateClick(Sender: TObject);
begin
{ Copy the contents of the form controls to the PersonRec and write
it to the stream }
with PersonRec do
begin
FirstName := edtFirstName.Text;
LastName := edtLastName.Text;
MI := edtMI.Text;
BirthDay := dtpBirthDay.Date;
Age := StrToInt(meAge.Text);
end;
RecordStream.WriteRec(PersonRec);
end;
procedure TMainForm.btnFirstClick(Sender: TObject);
begin
{ Go to the first record in the stream and display it as long as
there are records that exist in the stream }
if RecordStream.NumRecs <> 0 then
begin
RecordStream.First;
ShowCurrentRecord;
end;
end;
procedure TMainForm.btnNextClick(Sender: TObject);
begin
// Go to the next record as long as records exist in the stream
if RecordStream.NumRecs <> 0 then
begin
RecordStream.NextRec;
ShowCurrentRecord;
end;
end;
procedure TMainForm.btnLastClick(Sender: TObject);
begin
{ Go to the last record in the stream as long as there are records
in the stream }
if RecordStream.NumRecs <> 0 then
begin
RecordStream.Last;
ShowCurrentRecord;
end;
end;
procedure TMainForm.btnPrevClick(Sender: TObject);
begin
{ Go to the previous record in the stream as long as there are records
in the stream }
if RecordStream.NumRecs <> 0 then
begin
RecordStream.PreviousRec;
ShowCurrentRecord;
end;
end;
procedure TMainForm.btnClearClick(Sender: TObject);
begin
// Clear all controls on the form
edtFirstName.Text := '';
edtLastName.Text := '';
edtMI.Text := '';
meAge.Text := '';
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -