unit uEditor;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Grids, StdCtrls;
type
TfrmEditor =
class(TForm)
sgTable: TStringGrid;
MainMenu: TMainMenu;
Datei1: TMenuItem;
Neu1: TMenuItem;
ffnen1: TMenuItem;
Speichernunter1: TMenuItem;
N1: TMenuItem;
Schlieen1: TMenuItem;
SaveDialog: TSaveDialog;
lblArticle_Number: TLabel;
lblArticle_Description: TLabel;
lblArticle_Price: TLabel;
lblArticle_Category: TLabel;
btnAdd: TButton;
OpenDialog: TOpenDialog;
procedure Neu1Click(Sender: TObject);
procedure position(dataset : Integer);
procedure btnAddClick(Sender: TObject);
procedure Speichernunter1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ffnen1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type Articles =
record
ID : Integer;
Item :
String[255];
Category :
String[255];
Price : Integer;
end;
var
frmEditor: TfrmEditor;
rFile :
file of Articles;
const
BufferSize = 32;
implementation
{$R *.dfm}
procedure TfrmEditor.btnAddClick(Sender: TObject);
var
Buffer :
Array[0..BufferSize - 1]
of Articles;
i, p : Integer;
begin
// Tabelle erweitern
sgTable.RowCount := sgTable.RowCount + 1;
Seek(rFile, 0);
p := 0;
while not eof(rFile)
do begin
Read(rFile, Buffer[p]);
inc(p);
end;
CloseFile(rFile);
Buffer[p + 1].ID := 0;
Buffer[p + 1].Item := '
';
Buffer[p + 1].Category := '
';
Buffer[p + 1].Price := 0;
Rewrite(rFile);
for i := 0
to p
do Write(rFile, Buffer[i]);
position(FileSize(rFile)-1);
end;
procedure TfrmEditor.ffnen1Click(Sender: TObject);
var
dataset : Articles;
filename :
String;
begin
if OpenDialog.Execute
then
begin
filename := OpenDialog.filename;
AssignFile(rFile, filename);
end;
if FileExists(filename)
then Reset(rFile);
position(0);
position(1);
position(2);
end;
procedure TfrmEditor.FormCreate(Sender: TObject);
begin
sgTable.Options + [goAlwaysShowEditor,goEditing];
sgTable.EditorMode := true;
end;
procedure TfrmEditor.Neu1Click(Sender: TObject);
var filename :
String;
begin
if SaveDialog.Execute
then
begin
filename := SaveDialog.Filename;
AssignFile(rFile, filename);
if FileExists(filename)
then
Reset(rFile)
else Rewrite(rFile);
end;
end;
procedure TfrmEditor.position(dataset : Integer);
var curDataset : Articles;
begin
if (dataset < 0)
or (dataset >= FileSize(rFile))
then exit
else
begin
seek(rFile, dataset);
read(rFile, curDataset);
sgTable.Cells[0,dataset] := IntToStr(curDataset.ID);
sgTable.Cells[1,dataset] := curDataset.Item;
sgTable.Cells[2,dataset] := curDataset.Category;
sgTable.Cells[3,dataset] := IntToStr(curDataset.Price);
end;
end;
procedure TfrmEditor.Speichernunter1Click(Sender: TObject);
var
dataset : Articles;
pos : Integer;
begin
Seek(rFile, FilePos(rFile)-1);
pos := FilePos(rFile);
dataset.ID := StrToInt(sgTable.Cells[0, pos]);
dataset.Item := sgTable.Cells[1, pos];
dataset.Category := sgTable.Cells[2, pos];
dataset.Price := StrToInt(sgTable.Cells[3, pos]);
Write(rFile, dataset);
position(pos);
end;
end.