unit Form.Main;
interface
uses
System.Generics.Collections,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TCountry =
class
private
FCountryCode: Integer;
FName:
string;
public
constructor Create(
const ACountryCode: Integer;
const AName:
string );
property CountryCode: Integer
read FCountryCode;
property name:
string read FName;
end;
TConverter =
class
private
FCountryCode: Integer;
FName:
string;
public
constructor Create(
const ACountryCode: Integer;
const AName:
string );
property CountryCode: Integer
read FCountryCode;
property name:
string read FName;
end;
TForm1 =
class( TForm )
ConverterListBox: TListBox;
CountryComboBox: TComboBox;
procedure CountryComboBoxChange( Sender: TObject );
procedure ConverterListBoxClick( Sender: TObject );
private
FCountries: TList<TCountry>;
FSelectedCountry: TCountry;
FConverters: TList<TConverter>;
FSelectedConverters: TList<TConverter>;
FSelectedConverter: TConverter;
procedure PopulateCountries;
procedure PopulateSelectedConverters;
public
procedure AfterConstruction;
override;
procedure BeforeDestruction;
override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
EnumQuery;
{ TForm1 }
procedure TForm1.AfterConstruction;
begin
inherited;
FCountries := TObjectList<TCountry>.Create( );
FCountries.Add( TCountry.Create( 1, '
Deutschland' ) );
FCountries.Add( TCountry.Create( 2, '
USA' ) );
FConverters := TObjectList<TConverter>.Create( );
FConverters.Add( TConverter.Create( 1, '
Meter' ) );
FConverters.Add( TConverter.Create( 1, '
Kilometer' ) );
FConverters.Add( TConverter.Create( 2, '
Meilen' ) );
FConverters.Add( TConverter.Create( 2, '
Inch' ) );
FSelectedConverters := TList<TConverter>.Create( );
PopulateCountries;
PopulateSelectedConverters;
end;
procedure TForm1.BeforeDestruction;
begin
inherited;
FreeAndNil( FSelectedConverters );
FreeAndNil( FConverters );
FreeAndNil( FCountries );
end;
procedure TForm1.ConverterListBoxClick( Sender: TObject );
begin
if ConverterListBox.ItemIndex < 0
then
FSelectedConverter :=
nil
else
FSelectedConverter := FSelectedConverters[ ConverterListBox.ItemIndex ];
end;
procedure TForm1.CountryComboBoxChange( Sender: TObject );
var
Item: TConverter;
begin
if CountryComboBox.ItemIndex < 0
then
FSelectedCountry :=
nil
else
FSelectedCountry := FCountries[ CountryComboBox.ItemIndex ];
FSelectedConverter :=
nil;
FSelectedConverters.Clear;
FSelectedConverters.AddRange( TEnumQuery.SelectFrom<TConverter>( FConverters,
function( AConverter: TConverter ): Boolean
begin
Result := Assigned( FSelectedCountry )
and ( AConverter.CountryCode = FSelectedCountry.CountryCode );
end ) );
PopulateSelectedConverters;
end;
procedure TForm1.PopulateCountries;
var
Item: TCountry;
begin
CountryComboBox.Items.BeginUpdate;
try
CountryComboBox.Items.Clear;
for Item
in FCountries
do
begin
CountryComboBox.Items.Add( Item.
name );
end;
CountryComboBox.ItemIndex := FCountries.IndexOf( FSelectedCountry );
finally
CountryComboBox.Items.EndUpdate;
end;
end;
procedure TForm1.PopulateSelectedConverters;
var
Item: TConverter;
begin
ConverterListBox.Items.BeginUpdate;
try
ConverterListBox.Items.Clear;
for Item
in FSelectedConverters
do
begin
ConverterListBox.Items.Add( Item.
name );
end;
ConverterListBox.ItemIndex := FSelectedConverters.IndexOf( FSelectedConverter );
finally
ConverterListBox.Items.EndUpdate;
end;
end;
{ TCountry }
constructor TCountry.Create(
const ACountryCode: Integer;
const AName:
string );
begin
inherited Create;
FCountryCode := ACountryCode;
FName := AName;
end;
{ TConverter }
constructor TConverter.Create(
const ACountryCode: Integer;
const AName:
string );
begin
inherited Create;
FCountryCode := ACountryCode;
FName := AName;
end;
end.