How do I know which DB-Aware controls are associated with TDataSource?

I have DataSource1 (TDataSource) , and there are some DB-Aware controls associated with it (via SomeDBControl.DataSource=DataSource1 )

How can I find out (list) in the code which controls are associated with this TDataSource?

+6
source share
1 answer

The code below that uses RTTI works for me in D7 to display components that have the DataSource or MasterSource property by recursively searching for the container object (i.e. the form and its components).

(Obviously, you could do the same for any other forms / datamodules that interest you)

Update # 1: The original version of this answer prepared a list of each component in the form and the name of its DataSource / MasterSource, if any. I modified it to provide a better match with what you ask for in your body q).

Update # 2: This fixes several misses in Update # 1 and reimplementes the HasDataSource function in such a way as to avoid throwing an exception when parsing components that do not have the DataSource / MasterSource property.

 function HasDataSource(AComponent : TComponent; var ADataSource : TDataSource) : Boolean; function GetDataSource(APropName : String) : TDataSource; var AObject : TObject; PInfo : PPropInfo; begin Result := Nil; PInfo := GetPropInfo(AComponent, APropName); if PInfo = Nil then exit; AObject := GetObjectProp(AComponent, PInfo); Result := TDataSource(AObject); end; begin Result := False; ADataSource := GetDataSource('DataSource'); if ADataSource <> Nil then Result := True; if Result then exit; ADataSource := GetDataSource('MasterSource'); if ADataSource <> Nil then Result := True; end; procedure TForm1.Log(Msg: String); begin Memo1.Lines.Add(Msg); end; procedure TForm1.FindDataSourceObjects(AContainer : TComponent); var i : Integer; ADataSource : TDataSource; procedure LogDataSourceName(AContainer : TComponent); begin Log(AContainer.Name + ' Datasource: ' + ADataSource.Name); end; begin if HasDataSource(AContainer, ADataSource) then LogDataSourceName(AContainer); for i := 0 to AContainer.ComponentCount - 1 do begin FindDataSourceObjects(AContainer.Components[i]); end; end; procedure TForm1.btnFindClick(Sender: TObject); begin FindDataSourceObjects(Self); end; 

DFM of my form

 object Form1: TForm1 Left = 195 Top = 124 Width = 623 Height = 303 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object DBText1: TDBText Left = 307 Top = 56 Width = 65 Height = 17 DataSource = DataSource1 end object Panel1: TPanel Left = 307 Top = 80 Width = 281 Height = 161 Caption = 'Panel1' TabOrder = 0 object DBText2: TDBText Left = 24 Top = 64 Width = 65 Height = 17 DataSource = DataSource2 end end object Memo1: TMemo Left = 8 Top = 16 Width = 281 Height = 225 TabOrder = 1 end object btnFind: TButton Left = 307 Top = 16 Width = 75 Height = 25 Caption = 'Find' TabOrder = 2 OnClick = btnFindClick end object DataSource1: TDataSource DataSet = ClientDataSet1 Left = 448 Top = 16 end object DataSource2: TDataSource DataSet = ClientDataSet2 Left = 544 Top = 16 end object ClientDataSet1: TClientDataSet Aggregates = <> Params = <> Left = 408 Top = 16 end object ClientDataSet2: TClientDataSet Aggregates = <> MasterSource = DataSource1 PacketRecords = 0 Params = <> Left = 496 Top = 16 end end 
+2
source

Source: https://habr.com/ru/post/971222/


All Articles