Entwickler-Ecke

Windows API - FindComponent geht nicht bei LZ-erstellen Kompos [gelöst]


recall - So 07.09.03 12:25
Titel: FindComponent geht nicht bei LZ-erstellen Kompos [gelöst]
Hallo,


Delphi-Quelltext
1:
2:
3:
4:
{Formular bestimmen}
    F := TForm(FindGlobalComponent(FormPar));
{Objekt bestimmen}
    Obj := F.FindComponent(Par);

Par ist TForm.Name und FormPar ist TControl.Name (vom Typ her)...

Aber das geht nur bei zur Entworfszeit erstellten Kompos. Wenn ich eine Form mit Create zur Laufzeit erstelle (oder irgendeine andere Komponente), dann findet FindComponent und FindGlobalComponent diese Kompo nicht :(

Wer weiss was ??

Viele Grüsse.


Keldorn - So 07.09.03 12:43

klar geht Findcomponent auch mit zur laufzeit erstellten kompos. DORT brauchst du es ja gerade, wenn du deine Kompos wiederfinden willst und die nicht in einem array hast.

poste mal deinen erstellenCode.


recall - So 07.09.03 15:16

Hallo,

Ich mache in etwa folgendes:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
    Obj := TT.Create(Self);
    Obj.Name := XNewName(XType, FormPar);

[...]

     TTParent := TWinControl(FindComp(FindGlobalComponent(FormPar), Par));
      if TTParent<>nil then
        Obj.Parent := TTParent
      else Obj.Parent := TWinControl(FindGlobalComponent(FormPar));

  {properties setzen !}
      if XProperties<>nil then
        For I := 0 to XProperties.Count-1 do
          XSetProperty(FormPar + '.' + Obj.Name, XProperties[I], XValues[I]);

[...]

function TForm1.XSetProperty(XComponent, XProperty, XValue: String): Boolean;
var
  Obj: TComponent;
  F: TForm;
  I: Word;
  FormPar, Par: String;
begin
  Result := False;
  try
{Aus  XCpmponent die FormPar und Par extrahieren}
    I := 1;
    FormPar := '';
    Repeat
      if I<Length(XComponent)+1 then
        if XComponent[I]<>'.' then
          FormPar := FormPar + XComponent[I];
      Inc(I);
    Until ((XComponent[I]='.'or (I>=Length(XComponent)+1));
    Inc(I);
    Par := '';
    Repeat
      if I<Length(XComponent)+1 then
        Par := Par + XComponent[I];
      Inc(I);
    Until (I>=Length(XComponent)+1);

{Formular bestimmen}
    F := TForm(FindGlobalComponent(FormPar));
{Objekt bestimmen}
    Obj := F.FindComponent(Par);

{Falls direkt die Formular-Eigenschaft gesetzt werden soll:}
    if (F is TForm) and (Obj=nilthen                                       {hier Obj=nil}
    begin
      XProperty := FormPar + '.' + XProperty;
      E_Props.SetProp(XProperty, XValue);
      Result := True;
    end;

Wie im Sourcecode markiert, wird Obj kein Wert zugewiesen. :(
Was tun ?

Den Namen (also die String-Variable Par) habe ich überprüft. Stimmt mit der erstellten Kompo überein. Diese wird auch sichtbar in der Form, aber ich kann sie mit FindComponent nicht finden :(
Achso, das Formular (wurde auch zur Laufzeit erstellt) findet er aber mit FindGlobalComponent.

Viele Grüsse.

//Edit:
Zitat:
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
grummel :evil:


recall - So 07.09.03 17:00

Hallo Leute, ich habs :D !!
Man braucht:

Delphi-Quelltext
1:
2:
3:
        F := TForm(FindGlobalComponent(FormPar));
        Obj.Parent := F;
        F.InsertComponent(Obj);    {<--- das ist wichtig, sonst kann er sie nicht finden}
... dann gehts !

Viele Grüsse, und Dank an alle.

Muss mal ein Tut schreiben, wenn ich fertig bin :D !