unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
btnStart: TButton;
btnOK: TButton;
Label1: TLabel;
Label2: TLabel;
lblCategory1: TLabel;
lblCategory2: TLabel;
lblItem1: TLabel;
lblItem2: TLabel;
lbxCategory1: TListBox;
lbxCategory2: TListBox;
lbxItem1: TListBox;
lbxItem2: TListBox;
procedure btnOKClick(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lbxCategory1SelectionChange(Sender: TObject; User: boolean);
procedure lbxCategory2SelectionChange(Sender: TObject; User: boolean);
procedure lbxItem1SelectionChange(Sender: TObject; User: boolean);
private
procedure LoadCategory(AListBox: TListBox);
end;
var
Form1: TForm1;
ListOfFileNames: TStringList;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnOKClick(Sender: TObject);
begin
if lbxItem1.ItemIndex < 0 then
begin
ShowMessage('Please tell me what you want to eat tonight.');
Exit;
end;
if lbxItem2.ItemIndex < 0 then
begin
ShowMessage('You haven''t told me what you want to eat tomorrow morning.');
Exit;
end;
ShowMessage('I''m now prepraring the food you''ve ordered.' + LineEnding +
'See you later!');
Close;
end;
procedure TForm1.btnStartClick(Sender: TObject);
begin
btnStart.Visible := False;
lblCategory1.Visible := True;
lbxCategory1.Visible := True;
lblItem1.Visible := True;
lbxItem1.Visible := True;
LoadCategory(lbxCategory1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListOfFileNames := TStringList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ListOfFileNames.Free;
end;
procedure TForm1.lbxCategory1SelectionChange(Sender: TObject; User: boolean);
var
FileName : string;
InputFile : TextFile;
S : string;
begin
if lbxCategory1.ItemIndex < 0 then
Exit;
FileName := ListOfFileNames[lbxCategory1.ItemIndex];
if not(FileExists(FileName)) then
begin
ShowMessage('Missing ' + '!');
Exit;
end;
// Preparing
lbxItem1.Clear;
AssignFile(InputFile, FileName);
Reset(InputFile);
// Loading
while not EOF(InputFile) do
begin
ReadLn(InputFile, S);
lbxItem1.AddItem(S, nil);
end;
// Finishing
CloseFile(InputFile);
end;
procedure TForm1.lbxCategory2SelectionChange(Sender: TObject; User: boolean);
var
FileName : string;
InputFile : TextFile;
S : string;
begin
if lbxCategory2.ItemIndex < 0 then
Exit;
FileName := ListOfFileNames[lbxCategory2.ItemIndex];
if not(FileExists(FileName)) then
begin
ShowMessage('Missing ' + '!');
Exit;
end;
// Preparing
lbxItem2.Clear;
AssignFile(InputFile, FileName);
Reset(InputFile);
// Loading
while not EOF(InputFile) do
begin
ReadLn(InputFile, S);
lbxItem2.AddItem(S, nil);
end;
// Finishing
CloseFile(InputFile);
end;
procedure TForm1.lbxItem1SelectionChange(Sender: TObject; User: boolean);
begin
if (Label2.Visible) then
Exit;
Label2.Visible := True;
lblCategory2.Visible := True;
lbxCategory2.Visible := True;
lblItem2.Visible := True;
lbxItem2.Visible := True;
LoadCategory(lbxCategory2);
end;
procedure TForm1.LoadCategory(AListBox: TListBox);
const
CategoryFileName = 'category.txt';
Separator = '|';
var
InputFile : TextFile;
SplitPos : Integer;
S : string;
begin
if not(FileExists(CategoryFileName)) then
begin
ShowMessage('Missing category.txt!');
Exit;
end;
// Preparing
ListOfFileNames.Clear;
AListBox.Clear;
AssignFile(InputFile, CategoryFileName);
Reset(InputFile);
// Loading
while not EOF(InputFile) do
begin
ReadLn(InputFile, S);
SplitPos := S.IndexOf(Separator);
ListOfFileNames.Add(Trim(S.Substring(0, SplitPos)));
AListBox.AddItem(S.Remove(0, SplitPos+2), nil);
end;
// Finishing
CloseFile(InputFile);
end;
end.