Autor Beitrag
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: So 20.02.05 22:12 
Abend,

Ich habe hier ein Problem, dass ich gerne auf meinem Weg lösen würde.

Ich erstelle ein TMemo und eine TFileListbox dynamisch:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
 fl : TFileListBox;
 Me : TMemo;
begin
 with fl do
  begin
   TFileListBox.Create(form1);
   parent := Form1;
   name : 'FLB';
  end;
 with Me do
  begin
   TMemo.Create(form1);
   parent := Form1;
   name := 'Mem';
  end;
end;


Da ich häufig mit diesen beiden Componenten arbeite habe ich mir eine Funktion geschrieben, z.B. beim adden eines Items:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function FL1 : TFileListBox;
var i : integer;
begin
 for i := 0 to ComponentsCount -1 do
  begin
   if Components[i].Name = 'FLB' then
    Result := TFileListBox(Components[i]);
  end;
end;

procedure ButtonClick();
begin
 FL1.items.add('Hallo');
end;


Funktioniert auch. Ich könnte genauso eine Funktion natürlich auch noch für das Memo schreiben, aber ich will das in eine Funktion packen. So habe ich sie mir gedacht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function MemoListbox(AName: String; Typ : TComponent):TComponent;
var i : integer;
begin
 for i := 0 to Components.Count-1 do
  begin
   if Components[i].name = AName then
    Result := Typ(Components[i]);
  end;
end;

procedure buttonclick();
begin
 MemoListBox(Fl, TMemo).lines.add('hallo');
end;


Jo das klappt so natürlich nicht, weil ich weiss nicht wie, ich das Result dynamisch casten kann.

danke.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: So 20.02.05 22:40 
Deine Funktion macht doch nichts anderes als das was FindComponent auch macht.

Also mein Vorschlag, benutze FindComponent und die Operatoren is bzw. as.

_________________
Ciao, Sprint.
Karlson Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: So 20.02.05 23:06 
oh mann.

Findcompononent hab ich vergessen :roll:

Jop so klappt das. danke.