home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / TI2938.ZIP / TI2938.ASC < prev   
Encoding:
Text File  |  1996-06-24  |  4.9 KB  |  143 lines

  1.    NUMBER  :  2938
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  1.0
  4.        OS  :  Windows
  5.      DATE  :  June 24, 1996                           
  6.  
  7.     TITLE  :  Creating Dynamic Components at Runtime
  8.  
  9. Using Delphi forms and components is simple.  When coupled with the 
  10. object inspector, controlling those objects requires little effort.  
  11. Creating these types dynamically is also not difficult.  This document
  12. is intended to give you some tips and hints on how to make dynamic
  13. component work for you.
  14.  
  15. (please note this term "dynamically" is subjective, behind the scenes, 
  16. Delphi creates all objects dynamically.  The information presented herein
  17. is for the programmer setting creation/properties/deletion the given 
  18. type at run time)
  19.  
  20. All types (a form or a component) can be created dynamically. To do 
  21. this, one needs to put a declaration in the VAR section of their code.
  22. This does not create an instance of the object, it creates a pointer.
  23. This pointer resides in the data segment (if the variable is declared
  24. globally) or the stack (if the variable is declared local to a procedure
  25. or function).  In order to instantiate this class, you must call the
  26. constructor.  This will allocate memory in the computers global heap
  27. for the class instance.  Trying to access the component before
  28. allocating memory will produce a general protection fault.
  29.  
  30. The Create() constructor is a class method descended from the TObject 
  31. Class.  Create() returns a pointer.  This method may or may not take one 
  32. or more parameters.  For most components (all objects which descend from
  33. TComponent are referred to as components), the constructor takes one
  34. parameter, the "owner" of type TComponent.  
  35.  
  36. When dynamically creating a component, setting the Owner to "Self" 
  37. is the most common practice.  If you are in one of a form's methods,
  38. "Self" refers to that form in that context.  If the owner is a valid
  39. object, freeing that object will also free the "owned" component.
  40. Another common parameter is "Application".  This might be used for a
  41. visual component that will not be displayed to the program's users.
  42. However, most components do not require that you set a specific owner,
  43. so it is not uncommon to set the owner to Nil.  Keep in mind, though,
  44. that you will not be able to change the owner afterwards.  If you do
  45. pass Nil to a component's constructor, you must remember to call that
  46. component's Free method when you are through using the component.
  47.  
  48. After creation, but before they can be displayed, windowed components
  49. (those descending from TWinControl) require the Parent property to be set.
  50. At the time you set the Parent property, it's usually also a good time to
  51. set other properties of this components instance, including event
  52. handlers (ie, Width, Color, OnClick).
  53.  
  54. Event handlers are identical to those specified in the object inspector.
  55. Simply set the component's property name for the event you want to handle
  56. to name of the event handler method  you want invoked.  Example 1 below
  57. would call the method called "myclick" whenever the button is clicked.
  58. Please note this method will be sent the appropriate parameters, and its
  59. incoming parameter list must be exact.     
  60.  
  61. Example 1:
  62. var
  63.   b1 : TButton;
  64. begin
  65.   .
  66.   .
  67.   .
  68.   b1 := TButton.Create(Self);
  69.   with b1 do begin     
  70.     Left := 20;
  71.     Top := 20;
  72.     Width := 90;
  73.     Height := 50;
  74.     Caption :=  my button';
  75.     Parent := Form1;
  76.     OnClick :=  MyClick;  { a procedure I defined somewhere else }
  77.   end;
  78.   .
  79.   .
  80.   .
  81. end;
  82.  
  83. The next example demonstrates how to create a button at run time by 
  84. clicking a predefined button.  Note the different way the button has 
  85. been created.  Either way would work.   Also note the buttons that are
  86. created are not freed in this code, they will be freed when the form 
  87. is released.
  88.  
  89. unit Unit1;
  90. interface
  91.  
  92. uses
  93.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  94.   Forms, Dialogs, StdCtrls;
  95.  
  96. type
  97.   TForm1 = class(TForm)
  98.     Button1: TButton;
  99.     procedure Button1Click(Sender: TObject);
  100.   private
  101.     { Private declarations }
  102.   public
  103.     { Public declarations }
  104.   procedure myClick(Sender: TObject);
  105.   end;
  106.  
  107. var
  108.   Form1: TForm1;
  109.  
  110. const
  111.   i : integer = 0;
  112.  
  113. implementation
  114. {$R *.DFM}
  115.  
  116. procedure TForm1.myClick(Sender: TObject);
  117. begin
  118.   with Sender as TButton do
  119.     Self.Caption := ClassName + ' ' + Name;
  120. end;
  121.  
  122. procedure TForm1.Button1Click(Sender: TObject);
  123. begin
  124.   with TButton.Create(self) do begin
  125.     Left := 20;
  126.     Top := 30 + i;
  127.     Width := 120;
  128.     Height := 40;
  129.     Name := 'ThisButton' + IntToStr(i);
  130.     Caption := 'There' + IntToStr(i);
  131.     OnClick :=  MyClick;  { a procedure I defined somewhere else }
  132.     Parent := Form1;
  133.   end; {end with}
  134.   inc(i, 40);
  135. end; {end button1.click}
  136.  
  137. end.
  138.  
  139. DISCLAIMER: You have the right to use this technical information
  140. subject to the terms of the No-Nonsense License Statement that
  141. you received with the Borland product to which this information
  142. pertains.
  143.