unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Dialogs, StdCtrls, Spin,
ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
btnNew: TButton;
btnRead: TButton;
btnNewSave: TButton;
btnNewCancel: TButton;
btnReadLine: TButton;
btnReadCancel: TButton;
Edit1: TEdit;
Label1: TLabel;
Notebook1: TNotebook;
Page1: TPage;
Page2: TPage;
SpinEdit1: TSpinEdit;
procedure FormCreate(Sender: TObject);
procedure btnNewClick(Sender: TObject);
procedure btnReadClick(Sender: TObject);
procedure btnNewSaveClick(Sender: TObject);
procedure btnNewCancelClick(Sender: TObject);
procedure btnReadCancelClick(Sender: TObject);
procedure btnReadLineClick(Sender: TObject);
private
procedure SaveData(Data: string);
procedure AppendData(Data: string);
function GetTotal: integer;
function ReadData(Line: Integer): string;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
const
FileName = 'MyData.dat';
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Notebook1.Visible := False;
end;
procedure TForm1.btnNewClick(Sender: TObject);
begin
btnNew.Enabled := False;
btnRead.Enabled := False;
Notebook1.Visible := True;
Notebook1.PageIndex := 0;
Edit1.Text := '';
end;
procedure TForm1.btnReadClick(Sender: TObject);
begin
btnNew.Enabled := False;
btnRead.Enabled := False;
Notebook1.Visible := True;
Notebook1.PageIndex := 1;
SpinEdit1.MaxValue := GetTotal;
end;
procedure TForm1.btnNewSaveClick(Sender: TObject);
var
Count: Integer;
begin
if (Edit1.Text = '') then
begin
ShowMessage('Cannot save empty data.');
Exit;
end;
Count := GetTotal;
if (Count <= 0) then
SaveData(Edit1.Text)
else
AppendData(Edit1.Text);
Edit1.Text := '';
end;
procedure TForm1.btnNewCancelClick(Sender: TObject);
begin
Notebook1.Visible := False;
btnNew.Enabled := True;
btnRead.Enabled := True;
end;
procedure TForm1.btnReadLineClick(Sender: TObject);
var
Index: Integer;
S: string;
begin
Index := SpinEdit1.Value;
S := ReadData(Index);
if (S <> '') then ShowMessage(S);
end;
procedure TForm1.btnReadCancelClick(Sender: TObject);
begin
Notebook1.Visible := False;
btnNew.Enabled := True;
btnRead.Enabled := True;
end;
procedure TForm1.SaveData(Data: string);
var
DataFile: TextFile;
begin
AssignFile(DataFile, ProgramDirectory + FileName);
{$I+}
try
Rewrite(DataFile);
WriteLn(DataFile, Data);
CloseFile(DataFile);
except
ShowMessage('Error saving data.');
end;
end;
procedure TForm1.AppendData(Data: string);
var
DataFile: TextFile;
begin
AssignFile(DataFile, ProgramDirectory + FileName);
try
Append(DataFile);
WriteLn(DataFile, Data);
CloseFile(DataFile);
except
ShowMessage('Error saving data.');
end;
end;
function TForm1.GetTotal: integer;
var
DataFile: TextFile;
Index: Integer;
S: string;
begin
Result := 0;
AssignFile(DataFile, ProgramDirectory + FileName);
{$I-}
Reset(DataFile);
{$i+}
If (IOResult <> 0) then Exit;
Index := 0;
while not EOF(DataFile) do
begin
Inc(Index);
ReadLn(DataFile, S);
end;
CloseFile(DataFile);
Result := Index;
end;
function TForm1.ReadData(Line: Integer): string;
var
DataFile: TextFile;
Index: Integer; // start from 1
S: string;
begin
Result := '';
AssignFile(DataFile, ProgramDirectory + FileName);
try
Reset(DataFile);
Index := 0;
while not EOF(DataFile) do
begin
Inc(Index);
ReadLn(DataFile, S);
if (Index = Line) then Break;
end;
CloseFile(DataFile);
Result := S;
except
ShowMessage('Error reading data.'+#13+'Make sure it is not empty.');
end;
end;
end.