unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, Grids;
type
{ TForm1 }
TForm1 = class(TForm)
btnGroupAdd: TButton;
btnRowAdd: TButton;
ListBox1: TListBox;
StringGrid1: TStringGrid;
procedure btnGroupAddClick(Sender: TObject);
procedure btnRowAddClick(Sender: TObject);
procedure btnRowDeleteClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1SelectionChange(Sender: TObject; User: boolean);
private
procedure GroupAdd(const Group: string);
procedure RowAdd(GroupIndex: Integer; const AName, ADescription: string);
procedure ShowData;
end;
const
NameLength = 10;
DescLength = 30;
type
TDataItem = record
Name : string[NameLength];
Description : string[DescLength];
end;
TDataList = array of TDataItem;
var
Form1 : TForm1;
Data : array of TDataList;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// Prepare StringGrid1
with StringGrid1 do
begin
ColCount := 2;
FixedCols := 0;
FixedRows := 0;
RowCount := 0;
Options := Options + [goRowSelect];
end;
// Some sample data
GroupAdd('Fruits');
RowAdd(0, 'Orange', 'Good source of vit C' );
RowAdd(0, 'Banana', 'Rich in potassium');
RowAdd(0, 'Apple', 'Crunchy');
GroupAdd('Animal');
RowAdd(1, 'Cheetah', 'Fastes land animal');
RowAdd(1, 'Bird', 'Can fly');
GroupAdd('Flower');
RowAdd(2, 'Roses', 'Are red');
RowAdd(2, 'Violets', 'Are blue');
ListBox1.ItemIndex := 0;
end;
procedure TForm1.ListBox1SelectionChange(Sender: TObject; User: boolean);
begin
ShowData;
end;
procedure TForm1.GroupAdd(const Group: string);
var
Count : Integer;
S : string;
begin
S := Group.Trim;
if S = '' then Exit;
Count := Length(Data);
SetLength(Data, Count+1);
ListBox1.Items.Add(S);
end;
procedure TForm1.RowAdd(GroupIndex: Integer; const AName, ADescription: string);
var
GroupedItems : ^TDataList;
ItemName, ItemDesc : string;
Count : Integer;
begin
// Bad input?
if (GroupIndex < 0) or (GroupIndex > Length(Data)-1) then Exit;
ItemName := LeftStr(AName.Trim, NameLength);
ItemDesc := LeftStr(ADescription.Trim, DescLength);
if (ItemName = '') or (ItemDesc = '') then Exit;
// Store the data
GroupedItems := @(Data[GroupIndex]);
Count := Length(GroupedItems^);
SetLength(GroupedItems^, Count+1);
GroupedItems^[Count].Name := ItemName;
GroupedItems^[Count].Description := ItemDesc;
end;
procedure TForm1.ShowData;
var
GroupedItems : ^TDataList;
Count, i : Integer;
begin
if (ListBox1.ItemIndex < 0) then Exit;
StringGrid1.Clear;
GroupedItems := @(Data[ListBox1.ItemIndex]);
Count := Length(GroupedItems^);
StringGrid1.RowCount := Count;
for i := 0 to Count-1 do
begin
StringGrid1.Cells[0, i] := GroupedItems^[i].Name;
StringGrid1.Cells[1, i] := GroupedItems^[i].Description;
end;
StringGrid1.AutoSizeColumns;
end;
procedure TForm1.btnGroupAddClick(Sender: TObject);
var
S: string;
Z: Integer;
begin
S := InputBox('New Group', 'Group Name: ', '');
// Empty name is not allowed
S := S.Trim;
if S = '' then Exit;
// Exact duplicate is not allowed
Z := ListBox1.Items.IndexOf(S);
if Z >= 0 then
if ListBox1.Items[Z] = S then Exit;
GroupAdd(S);
end;
procedure TForm1.btnRowAddClick(Sender: TObject);
var
ItemName : string;
ItemDesc : string;
begin
if (ListBox1.ItemIndex < 0) then Exit;
ItemName := InputBox('Add New Item', 'Item Name: ', '');
ItemName := LeftStr(ItemName.Trim, NameLength);
if ItemName = '' then Exit;
ItemDesc := InputBox('Add New Item', 'Item Description: ', '');
ItemDesc := LeftStr(ItemDesc.Trim, DescLength);
if ItemDesc = '' then Exit;
RowAdd(ListBox1.ItemIndex, ItemName, ItemDesc);
ShowData;
end;
procedure TForm1.btnRowDeleteClick(Sender: TObject);
var
Z: Integer;
begin
ShowData; exit;
if (StringGrid1.Row <= 0) then Exit;
Z := StringGrid1.Cells[1, StringGrid1.Row].ToInteger;
Dec(Z);
StringGrid1.Cells[1, StringGrid1.Row] := Z.ToString;
end;
end.