unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//Deklaration der Klasse
type
TMyClass =
class
private
FName,
FBeschreibung,
FPfad:
string;
public
property Name:
string read FName
write FName;
property Beschreibung:
string read FBeschreibung
write FBeschreibung;
property Pfad:
string read FPfad
write FPfad;
end;
//Erzeugen von 3 Objekten und Einfügen in die Listbox
procedure TForm1.FormCreate(Sender: TObject);
var MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.
Name := '
Bla';
MyClass.Beschreibung := '
Erstes Objekt';
MyClass.Pfad := '
C:\Windows';
ListBox1.Items.AddObject(MyClass.
Name,MyClass);
MyClass := TMyClass.Create;
MyClass.
Name := '
Blubb';
MyClass.Beschreibung := '
Zweites Objekt';
MyClass.Pfad := '
C:\Windows\System';
ListBox1.Items.AddObject(MyClass.
Name,MyClass);
MyClass := TMyClass.Create;
MyClass.
Name := '
Wuppdi';
MyClass.Beschreibung := '
Drittes Objekt';
MyClass.Pfad := '
C:\Windows\System32';
ListBox1.Items.AddObject(MyClass.
Name,MyClass);
end;
//Freigeben der Objekte
procedure TForm1.FormDestroy(Sender: TObject);
var Current: integer;
begin
for Current := ListBox1.Items.Count - 1
downto 0
do
(ListBox1.Items.Objects[Current]
as TMyClass).Free;
end;
//Werte des angeklickten Objektes anzeigen
procedure TForm1.ListBox1Click(Sender: TObject);
var MyClass: TMyClass;
begin
if ListBox1.ItemIndex > -1
then
begin
MyClass := ListBox1.Items.Objects[ListBox1.ItemIndex]
as TMyClass;
if Assigned(MyClass)
then
ShowMessage(Format('
%s%s%s%s%s',[MyClass.
Name,sLineBreak,MyClass.Pfad,
sLineBreak,MyClass.Beschreibung]));
end;
end;
end.