democomponents.lpr 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. program democomponents;
  2. uses browserconsole, Classes;
  3. Type
  4. TMyGeneration = (first,second,third);
  5. { TMyParentComponent }
  6. TMyParentComponent = Class(TComponent)
  7. private
  8. FMyProperty: TMyGeneration;
  9. Published
  10. Property MyProperty : TMyGeneration Read FMyProperty Write FMyProperty;
  11. end;
  12. { TMyChildComponent }
  13. TMyChildComponent = Class(TMyParentComponent)
  14. Public
  15. destructor Destroy; override;
  16. end;
  17. Var
  18. DestroyCount : Integer;
  19. { TMyChildComponent }
  20. destructor TMyChildComponent.Destroy;
  21. begin
  22. DestroyCount:=DestroyCount+1;
  23. Writeln('Destroying child "',Name,'", current count : ',DestroyCount);
  24. inherited Destroy;
  25. end;
  26. Var
  27. P : TMyParentComponent;
  28. C : TMyChildComponent;
  29. Ci : TComponent;
  30. begin
  31. P:=TMyParentComponent.Create(Nil);
  32. try
  33. P.Name:='Parent1';
  34. P.MyProperty:=First;
  35. C:=TMyChildComponent.Create(P);
  36. C.Name:='Child1';
  37. C.MyProperty:=Second;
  38. C:=TMyChildComponent.Create(C);
  39. C.Name:='Child2';
  40. C.MyProperty:=Third;
  41. C:=TMyChildComponent.Create(P);
  42. C.Name:='Child3';
  43. C.MyProperty:=Second;
  44. Writeln('Components for loop');
  45. For CI in P do
  46. begin
  47. Write(CI.Name,', is child : ',C is TMyChildComponent,' ');
  48. If C is TMyChildComponent then
  49. Write('My property : ',TMyChildComponent(C).MyProperty);
  50. Writeln('');
  51. end;
  52. finally
  53. P.Destroy;
  54. end;
  55. end.