Autor Beitrag
Manfred Haidl
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: Sa 22.05.04 21:38 
Hallo,

ich habe probiert, Komponenten in einer Komponente zu erzeugen. Leider bekomme ich die "Unterkomponeten" nicht in der Objekthirachie angezeigt, und kann dadurch auch nicht die Properties im Objektinspektor bearbeiten.

Hier mein "Problemfall":
************************
ausblenden volle Höhe 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:
unit TestKomponente;

interface

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

type
  TUnterKomponente = class(TComponent)
    private
      IntTest: Integer;
    published
      property Test: Integer read IntTest write IntTest default 0;
  end;

  THauptKomponente = class(TComponent)
   private
      IntTest: Integer;
   public
    constructor Create(AOwner: TComponent); override;
   published
    UnterKomponente: TUnterKomponente;
    property Test: Integer read IntTest write IntTest default 0;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TestComponent', [THauptKomponente]);
end;

constructor THauptKomponente.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  UnterKomponente := TUnterKomponente.Create(Self);
  UnterKomponente.Name := 'MeineUnterKomponente';
end;

end.
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: Sa 22.05.04 21:49 
Hi,

schau dir mal die Prozedur SetSubComponent von TComponent an. Die solltest du in der Haupt Kompo aufrufen.

mfG
mirage228

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 07:10 
Danke für deinen Hinweis. Ich hatte das schon probiert und jetzt nocheinemal, die Unterkomponente bleibt aber weiter unsichtbar (bin schon nahe der Verzweifelung).
Hier der Code meines SetSubComponent-Aufrufs:
ausblenden Delphi-Quelltext
1:
2:
3:
  inherited Create(AOwner);
  UnterKomponente := TUnterKomponente.Create(Self);
  UnterKomponente.SetSubComponent(True);  // ****** Neue Codezeile!


mfg Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 07:18 
Hi,

du könntest probieren die Unterkomponente als published property von THauptKomponente zu verwenden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  THauptKomponente = class(TComponent) 
   private 
     IntTest: Integer; 
     FUnterKomponente: TUnterKomponente; 
   public 
     constructor Create(AOwner: TComponent); override; 
   published 
      property Test: Integer read IntTest write IntTest default 0; 
      property UnterKomponente: TUnterKomponente read FUnterKomponente write FUnterKomponente;
  end;


Im Konstruktur dann:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
constructor THauptKomponente.Create(AOwner: TComponent); 
begin 
  inherited Create(AOwner); 
  FUnterKomponente := TUnterKomponente.Create(Self); 
  FUnterKomponente.Parent := Self;
  FUnterKomponente.Name := 'MeineUnterKomponente'; 
  FUnterKomponente.SetSubComponent(True);
end;

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 08:38 
Hallo,

der Tipp bringt mich leider nicht weiter, weil hier weiterhin keine Anzeige in der Objekthirachie erfolgt. Das Problem ist, daß mir das "property UnterKomponente" nur die eine Unterkomponente zur Bearbeitung anzeigt. Da ich mehrere Unterkomponenten dynamisch erzeuge, ich also auch noch nicht dessen Anzahl kenne, hätte ich mir deshalb eine Auswahl in der Objekthirachie (das Fenster über dem Objektinspektor) vorgestellt. (So ähnlich wie wenn ich z.B. ein TLabel in ein TPanel einsetze)

Noch ein Hinweis zu der Codezeile:
ausblenden Delphi-Quelltext
1:
FUnterKomponente.Parent := Self;					

Parent ist eine (visuelle) Eigenschaft von TControl und in TComponent noch nicht vorhanden.

mfg. Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 08:49 
Manfred Haidl hat folgendes geschrieben:
Hallo,
Noch ein Hinweis zu der Codezeile:
ausblenden Delphi-Quelltext
1:
FUnterKomponente.Parent := Self;					

Parent ist eine (visuelle) Eigenschaft von TControl und in TComponent noch nicht vorhanden.

mfg. Manfred


Hi,

vielleicht ist genau das das Problem, dass nur visuelle Komponenten als Unterkomponente in der Objekthirarchie angezeigt werden!
Was passiert, wenn du die beiden Komponente einfach mal von TControl - anstatt TComponent - ableitest?

mfG
mirage228

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 10:07 
Hallo mirage228,

habe deinen Ratschlag mit TWinControl ausprobiert. Leider das Selbe, d.h. die Unterkomponenten bleiben unsichtbar. Das kann auch nicht ursächlich sein, weil Borland selbst auch in nichtvisuelle Komponenten dynamisch erzeugte Unterkomponenten anzeigt (s. z.B. TWebDispatcher).
Hast Du sonst noch einen Lösungsvorschlag?

mfg Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 11:38 
Hi,

habe mir mal den Source von TWebDispatcher angeschaut.

Dort wird der Konstruktor von Action mit dem Parameter aufgerufen, der den TWebDispatcher enthält.
Dieser wird ein privates Feld FWebDispatcher gespeichert. Es wird also eine Art "Owner" gesetzt.
Ausserdem gibt es noch die public-property
ausblenden Delphi-Quelltext
1:
property WebDispatcher: TCustomWebDispatcher read FWebDispatcher;					

Ausserdem handelt es sich bei den Actions nicht um eine Ableitung von TComponent, sondern von TCollection!

Also würde ich versuchen dieses Schema auf deine Komponenten anzuwenden.

mfG
mirage228

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 13:18 
Hallo mirage228,

erstmal vielen Dank für deine schnellen Antworten. Ich fürchte, dass ich deinen Vorschlag nicht verstehe. Wie schaut z.B. der Create-Aufruf der Unterkomponete im Code aus. Ich habe das Problem, dass man T-Component-Klassen nicht verwenden kann (z.B. Self, und so verstand ich Dich), da die Unterkomponente ja der TCollection-Klasse angehört. Vielleicht kannst Du mir ein größeres Code-Beispiel geben.

Gruß Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 14:16 
Hi,

ich habe es hingekriegt.

Hier der Quellcode:

ausblenden volle Höhe 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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
unit TestKomponent;

interface

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

type
  TUnterKomponente = class;
  THauptKomponente = class;

  TUnterKomponente = class(TCollection)
  private
    FIntTest: Integer;
    FHauptKomponente: THauptKomponente;
  protected
    function GetOwner: TPersistent; override;
  published
    property Test: Integer read FIntTest write FIntTest default 0;
    property HauptKomponente: THauptKomponente read FHauptKomponente;
  public
    constructor Create(aHauptKomponente: THauptKomponente;
      ItemClass: TCollectionItemClass);
  end;

  THauptKomponente = class(TComponent)
   private
      IntTest: Integer;
      FUnterKomponente: TUnterKomponente;
   public
     constructor Create(AOwner: TComponent); override;
   published
     property UnterKomponente: TUnterKomponente read FUnterKomponente;
     property Test: Integer read IntTest write IntTest default 0;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TestComponent', [THauptKomponente]);
end;

constructor TUnterKomponente.Create(aHauptKomponente: THauptKomponente;
  ItemClass: TCollectionItemClass);
begin
  inherited Create(ItemClass);
  FHauptKomponente := aHauptKomponente;
end;

function TUnterKomponente.GetOwner: TPersistent;
begin
  Result := FHauptKomponente;
end;

constructor THauptKomponente.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FUnterKomponente := TUnterKomponente.Create(Self, TCollectionItem);
end;

end.


Ich hoffe es hilft dir weiter. ;)

mfG
mirage228

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 17:17 
Hi mirage228,

Respekt! Aber ist das nicht ein etwas wirres Konstrukt? Trotzdem, ich habe es ausprobiert und es läuft soweit und es entspricht meinem eigentlichen Bedarf. Was noch nicht funktioniert, ist

1. das Erweitern der Items um eigene Eigenschaften (sind bisher auch leer). Ich habe nachgeschaut, konnte aber die richtige Klasse nicht finden.

2. werden die Items nicht in der Form abgespeichert, so dass sie beim Neueinlesen der Form wieder verschwunden sind.

Kannts Du mir hier auch noch auf die Sprünge helfen, bzw. wenn Du die Quelltexte hast nachschauen wie das funktionieren soll?

Mit bestem Dank
Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 18:41 
Hi,

hier nun die Unit, wo alles funktioniert ;)

Hat ne geweile gedauert, bis ich dahinter gestiegen bin ;)

ausblenden volle Höhe 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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
unit TestKomponent;

interface

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

type
  TUnterKomponente = class;
  THauptKomponente = class;

  TUnterItem = class(TCollectionItem)
  private
    FIntVal: Integer;
    FName: String;
  protected
    function GetDisplayName: string; override;
    procedure SetDisplayName(const Value: string); override;
  published
    property MyIntVal: Integer read FIntVal write FIntVal default 0;
    property Name: String read GetDisplayName write SetDisplayName;
  public
    constructor Create(Collection: TCollection); override;
    procedure Assign(Source : TPersistent); override;
    procedure AssignTo(Dest: TPersistent); override;
  end;

  TUnterKomponente = class(TCollection)
  private
    FIntTest: Integer;
    FHauptKomponente: THauptKomponente;
    function GetItem(Index: Integer): TUnterItem;
    procedure SetItem(Index: Integer; Value: TUnterItem);
  protected
    function GetOwner: TPersistent; override;
    function GetAttrCount: Integer; override;
    function GetAttr(Index: Integer): string; override;
    function GetItemAttr(Index, ItemIndex: Integer): string; override;
    procedure SetItemName(Item: TCollectionItem); override;
    procedure Update(Item: TCollectionItem); override;
  published
    property Test: Integer read FIntTest write FIntTest default 0;
  public
    property Items[Index: Integer]: TUnterItem read GetItem write SetItem; default;
    property HauptKomponente: THauptKomponente read FHauptKomponente;
    constructor Create(aHauptKomponente: THauptKomponente;
      ItemClass: TCollectionItemClass);
    function Add: TUnterItem;
  end;

  THauptKomponente = class(TComponent)
   private
      IntTest: Integer;
      FUnterKomponente: TUnterKomponente;
      function GetItem(Index: Integer): TUnterItem;
      procedure SetUnterKomponente(Value: TUnterKomponente);
   public
     constructor Create(AOwner: TComponent); override;
     property UnterItems[Index: Integer]: TUnterItem read GetItem;
   published
     property Test: Integer read IntTest write IntTest default 0;
     property UnterKomponente: TUnterKomponente read FUnterKomponente write SetUnterKomponente;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TestComponent', [THauptKomponente]);
end;

constructor TUnterItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  // Konstruktorcode...
end;

function TUnterItem.GetDisplayName: string;
begin
  Result := FName;
end;

procedure TUnterItem.SetDisplayName(const Value: string);
var
  I: Integer;
  Item: TUnterItem;
begin
  if AnsiCompareText(Value, FName) <> 0 then
  begin
    if Collection <> nil then
      for I := 0 to Collection.Count - 1 do
      begin
        Item := TUnterKomponente(Collection).Items[I];
        if (Item <> Self) and (Item is TUnterItem) and
          (AnsiCompareText(Value, Item.Name) = 0) then
          raise Exception.Create('Doppelter Name');
      end;
    FName := Value;
    Changed(False);
  end;
end;

procedure TUnterItem.Assign(Source: TPersistent);
begin
  if Source is TUnterItem then
    Self.MyIntVal := TUnterItem(Source).MyIntVal else
  inherited Assign(Source);
end;

procedure TUnterItem.AssignTo(Dest: TPersistent);
begin
  if Dest is TUnterItem then
    TUnterItem(Dest).MyIntVal := Self.MyIntVal
  else inherited AssignTo(Dest);
end;

constructor TUnterKomponente.Create(aHauptKomponente: THauptKomponente;
  ItemClass: TCollectionItemClass);
begin
  inherited Create(ItemClass);
  FHauptKomponente := aHauptKomponente;
end;

function TUnterKomponente.GetItem(Index: Integer): TUnterItem;
begin
  Result := inherited GetItem(Index) as TUnterItem;
end;

procedure TUnterKomponente.SetItem(Index: Integer; Value: TUnterItem);
begin
  inherited SetItem(Index, Value);
end;

function TUnterKomponente.GetOwner: TPersistent;
begin
  Result := FHauptKomponente;
end;

// Die Anzahl der Published Properties!
function TUnterKomponente.GetAttrCount: Integer;
begin
  Result := 2;
end;

// Name der Published Properties!
function TUnterKomponente.GetAttr(Index: Integer): string;
begin
  case Index of
    0: Result := 'Name';
    1: Result := 'MyIntVal';
  else
    Result := '';
  end;
end;

// Wert der Published Properties!
function TUnterKomponente.GetItemAttr(Index, ItemIndex: Integer): string;
begin
  case Index of
    0: Result := Items[ItemIndex].Name;
    1: Result := IntToStr(Items[ItemIndex].MyIntVal);
  else
    Result := '';
  end;
end;

procedure TUnterKomponente.SetItemName(Item: TCollectionItem);
var
  I, J: Integer;
  ItemName: string;
  CurItem: TUnterItem;
begin
  J := 1;
  while True do
  begin
    ItemName := Format('UnterKomponente%d', [J]);  { do not localize }
    I := 0;
    while I < Count do
    begin
      CurItem := Items[I] as TUnterItem;
      if (CurItem <> Item) and (CompareText(CurItem.Name, ItemName) = 0) then
      begin
        Inc(J);
        Break;
      end;
      Inc(I);
    end;
    if I >= Count then
    begin
      (Item as TUnterItem).Name := ItemName;
      Break;
    end;
  end;
end;

procedure TUnterKomponente.Update(Item: TCollectionItem);
begin
end;

function TUnterKomponente.Add: TUnterItem;
begin
  Result := inherited Add as TUnterItem;
end;

constructor THauptKomponente.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FUnterKomponente := TUnterKomponente.Create(Self, TUnterItem);
end;

function THauptKomponente.GetItem(Index: Integer): TUnterItem;
begin
  Result := FUnterKomponente[Index];
end;

procedure THauptKomponente.SetUnterKomponente(Value: TUnterKomponente);
begin
  FUnterKomponente.Assign(Value);
end;

end.


... aber es funktioniert ;)

mfG
mirage228

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 23.05.04 19:13 
Hallo mirage228,

Du hast das sehr schnell hingebracht und es funktioniert einwandfrei. Ich werde mich jetzt auch erstmal um das Codeverständis bemühen. Es hat sich ja zur einer komplexeren Sache entwickelt, als ich mir das bei meiner Anfrage vorstellte. Danke!

Viele Grüße aus einem heute verregneten Passau, Manfred
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: So 23.05.04 20:20 
Manfred Haidl hat folgendes geschrieben:
Hallo mirage228,

Du hast das sehr schnell hingebracht und es funktioniert einwandfrei. Ich werde mich jetzt auch erstmal um das Codeverständis bemühen. Es hat sich ja zur einer komplexeren Sache entwickelt, als ich mir das bei meiner Anfrage vorstellte. Danke!

Viele Grüße aus einem heute verregneten Passau, Manfred


Bitte sehr ^^

Und falls du noch Fragen hast :arrow: Einfach schreiben :)

mfG
mirage228

P.S.: Hier in Nord-Deutschland war es ziemlich windig und bewölkt, aber jetzt ist es mittlerweile ein wenig aufgeklart. Ich hoffe das Wetter bei euch bessert sich auch mal - wir haben ja schließlich schon fast
Sommer :wink:

_________________
May the source be with you, stranger.
Manfred Haidl Threadstarter
Hält's aus hier
Beiträge: 11

Win XP
D6 Pers, D7 Trail
BeitragVerfasst: So 06.06.04 18:27 
Hallo mirage228,

Du hast das sehr schnell hingebracht und es funktioniert einwandfrei. Ich werde mich jetzt auch erstmal um das Codeverständis bemühen. Es hat sich ja zur einer komplexeren Sache entwickelt, als ich mir das bei meiner Anfrage vorstellte. Danke!

Viele Grüße aus einem heute verregneten Passau, Manfred