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

  1.    NUMBER  :  2790
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  June 24, 1996                           
  6.  
  7.     TITLE  :  Creating & Deleting TFields at run-time
  8.  
  9. TField components (or more appropriately, descendants of the TField
  10. component of types corresponding to field types) can be created at design-
  11. time using the Fields Editor. The Fields Editor is invoked by double-
  12. clicking on the design icon for a TTable or TQuery component. But TField 
  13. descendants can also be created and deleted at run-time.
  14.  
  15. Descendants of the TField component (such as TStringField, TIntegerField, 
  16. etc.) are created by invoking the Create method for the type of TField 
  17. descendant appropriate to the field in the data set. That is, the Create 
  18. method for the TStringField descendant of TField would be called to create
  19. a TField descendant for a string-type field in the current data set. The 
  20. Create method requires one parameter, that of the owner of the TField 
  21. descendant, which is the containing TForm. After creating the TField 
  22. descendant component, a number of key properties need to be set in order
  23. to connect it with the field in the data set. These are:
  24.  
  25. FieldName: the name of the field in the table.
  26. Name:      a unique identifier for the TField descendant component.
  27. Index:     the TField descendant component's position in the array of 
  28.            TFields (the Fields property of the TTable or TQuery with which
  29.            the TField will be associated).
  30. DataSet:   the TTable or TQuery with which the TField will be associated.
  31.  
  32. The code snippet below demonstrates creating a TStringField. The 
  33. containing TForm is called Form1 (referred to here with the Self 
  34. variable), the active data set is a TQuery named Query1, and the field for
  35. which the TStringField component is being created is a dBASE table field
  36. named CO_NAME. This new TField descendant will be the second TField in the
  37. Fields array property of Query1. Note that the data set with which the new
  38. TField descendant will be associated (in this case, Query1) must be closed
  39. prior to adding the TField and then reopened afterwards.
  40.  
  41. procedure TForm1.Button2Click(Sender: TObject);
  42. var
  43.   T: TStringField;
  44. begin
  45.   Query1.Close;
  46.   T := TStringField.Create(Self);
  47.   T.FieldName := 'CO_NAME';
  48.   T.Name := Query1.Name + T.FieldName;
  49.   T.Index := Query1.FieldCount;
  50.   T.DataSet := Query1;
  51.   Query1.FieldDefs.UpDate;
  52.   Query1.Open;
  53. end;
  54.  
  55. The example above example creates a new TStringField named Query1CO_NAME.
  56.  
  57. Deleting an existing TField descendant is merely a matter of invoking the 
  58. Free method for that component. In the example below, the TForm's Find-
  59. Component method is used to return a pointer to the TStringField component 
  60. named Query1CO_NAME. The return value for the FindComponent will either be
  61. of type TComponent if successful or nil if unsuccessful. This return value
  62. can be used to determine whether the component actually exists prior to 
  63. invoking its Free method.
  64.  
  65. procedure TForm1.Button1Click(Sender: TObject);
  66. var
  67.   TC: TComponent;
  68. begin
  69.   TC := FindComponent('Query1CO_NAME');
  70.   if not (TC = nil) then begin
  71.     Query1.Close;
  72.     TC.Free;
  73.     Query1.Open;
  74.   end;
  75. end;
  76.  
  77. As with creating a TField, if the data set associated with the TField 
  78. descendant is currently active, it must be closed or deactivated prior to 
  79. invoking this method.
  80.  
  81.  
  82.  
  83. DISCLAIMER: You have the right to use this technical information
  84. subject to the terms of the No-Nonsense License Statement that
  85. you received with the Borland product to which this information
  86. pertains.
  87.