You can achieve that the easiest by putting all your TStrings of your class
into
an extra “Holder”-stringlist as objects.

A form with on it a TComboBox and a TListBox:

[CODE]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMyClass = Class(TObject)
  Private
    fMyLists : TStrings;
    fList1   : TStrings;
    fList2   : TStrings;
    fList3   : TStrings;
    Procedure FillList(Const AList : TStrings);
  Public
    Constructor Create;
    Destructor Destroy; Override;
    Property MyLists : TStrings Read fMyLists;
  End;

  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    fMyClass : TMyClass;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Procedure TMyClass.FillList(Const AList : TStrings);
Var
  I : Integer;
Begin
  For I := 0 To 9 Do
    Begin
      AList.Add(FormatDateTime('yyyy-mm-dd zzzz', Now));
      Sleep(15);
    End;
End;

Constructor TMyClass.Create;
Begin
  Inherited Create;
  fMyLists := TStringList.Create;
  fList1 := TStringList.Create;
  FillList(fList1);
  fList2 := TStringList.Create;
  FillList(fList2);
  fList3 := TStringList.Create;
  FillList(fList3);
  fMyLists.AddObject('List1', fList1);
  fMyLists.AddObject('List2', fList2);
  fMyLists.AddObject('List3', fList3);
End;

Destructor TMyClass.Destroy;
Begin
  fList3.Free;
  fList2.Free;
  fList1.Free;
  fMyLists.Free;
End;

//
============================================================================
=

procedure TForm1.FormCreate(Sender: TObject);
begin
  fMyClass := TMyClass.Create;
  ComboBox1.Items.Assign(fMyClass.MyLists);
  ComboBox1.ItemIndex := 0;
  ComboBox1Change(Nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  fMyClass.Free;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 
ListBox1.Items.Assign(TSTrings(ComboBox1.Items.Objects[ComboBox1.ItemIndex])
);
end;

end.
[/CODE]

Greetz,

Peter.

________________________________________
From: [email protected] [mailto:[email protected]] On Behalf
Of lv_solutions
Sent: donderdag 4 februari 2010 16:29
To: [email protected]
Subject: [delphi-en] Refrence TString by its name

  
Beginner,

I have dreated a class and have a few stringlist in the class.
How do I reference the lists by a name selected from a combobox?

Delphi 7

Thanks

Reply via email to