Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Durch ListBox1-Auswahl ListBox2-Auswahlmöglichkeiten festlegen

  Alt 1. Jun 2015, 19:03
Mal auf die Schnelle dahingetippt um das Prinzip zu zeigen

(Source und EXE im Anhang)
Delphi-Quellcode:
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.
Angehängte Dateien
Dateityp: zip dp_185336.zip (878,7 KB, 6x aufgerufen)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat