Mega Code Archive

 
Categories / Delphi / ADO Database
 

How to set the DataSource property to several db aware controls in one call

Title: How to set the DataSource property to several db-aware controls in one call If you need to change the DataSource property of many data-aware controls that do not have a common ancestor you can use Delphi's RTTI (run-time type info) and call only one procedure. Here's how: ~~~~~~~~~~~~~~~~~~~~~~~~~ uses TypInfo; ... procedure ApplyDataSource(dbCtrls: array of TControl; DS: TDataSource) ; var cnt: Integer; PropInfo: PPropInfo; begin for cnt := Low(dbCtrls) to High(dbCtrls) do begin PropInfo := GetPropInfo(dbCtrls[cnt].ClassInfo, 'DataSource') ; if Assigned(PropInfo) then SetOrdProp(dbCtrls[cnt], PropInfo, LongInt(DS)) ; end; end; ... Usage: ApplyDataSource([DBNavigator1, DBText1, DBButton1], DataSource1) ; ~~~~~~~~~~~~~~~~~~~~~~~~~