1
0
lazarus-tutorials/chemCollection/mainchemform.pas
2021-10-21 16:13:43 -04:00

100 lines
2.1 KiB
ObjectPascal

unit mainChemForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, RTTIGrids, grids;
type
TElementItem = class(TCollectionItem)
private
FNumber: cardinal;
FName: string;
FSymbol: string;
FGroup: string;
published
property AtomicNo: cardinal read FNumber write FNumber;
property Name: string read FName write FName;
property Symbol: string read FSymbol write FSymbol;
property Group: string read FGroup write FGroup;
end;
{ TChemForm }
TChemForm = class(TForm)
chemGrid: TTIGrid;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
Felements: TCollection;
procedure SetupChemGrid;
end;
var
ChemForm: TChemForm;
implementation
{$R *.lfm}
{ TChemForm }
procedure TChemForm.FormCreate(Sender: TObject);
begin
SetupChemGrid;
end;
procedure TChemForm.FormDestroy(Sender: TObject);
begin
// Free up the collection's memory!
Felements.Free;
end;
procedure TChemForm.SetupChemGrid;
procedure AddElement(aNumber: cardinal; const aName, aSymbol, aGroup: string);
var ei: TElementItem;
begin
ei := TElementItem(Felements.Add);
ei.AtomicNo := aNumber;
ei.Name := aName;
ei.Symbol := aSymbol;
ei.Group := aGroup;
end;
procedure AddElements;
begin
AddElement(1, 'Hydrogen', 'H', '"I"');
AddElement(2, 'Helium', 'He', '"II"');
AddElement(3, 'Lithium', 'Li', 'I');
AddElement(4, 'Beryllium', 'Be', 'II');
AddElement(5, 'Boron', 'B', 'III');
AddElement(6, 'Carbon', 'C', 'IV');
AddElement(7, 'Nitrogen', 'N', 'V');
AddElement(8, 'Oxygen', 'O', 'VI');
AddElement(9, 'Fluorine', 'F', 'VII');
AddElement(10, 'Neon', 'Ne', 'VIII');
end;
procedure InitalizeChemGrid;
begin
Self.Height := 310;
Self.Width := 340;
chemGrid.Align := alClient;
chemGrid.TIOptions := chemGrid.TIOptions + [tgoStartIndexAtOne];
chemGrid.Options := chemGrid.Options + [goDrawFocusSelected];
chemGrid.FixedColor := clMoneyGreen;
chemGrid.DefaultDrawing := True;
chemGrid.AutoFillColumns := True;
end;
begin
Felements := TCollection.Create(TElementItem);
AddElements;
InitalizeChemGrid;
chemGrid.ListObject := Felements;
end;
end.