delphi - How to find out which DB-Aware controls are linked to a TDataSource? -
delphi - How to find out which DB-Aware controls are linked to a TDataSource? -
i have datasource1 (tdatasource), , there db-aware controls linked (via somedbcontrol.datasource=datasource1)
how can find out (enumerate) in code controls linked given tdatasource?
the code below, uses rtti, works me in d7 list components have datasource or mastersource property recursively searching container object (i.e. form , components).
(obviously, similar other forms/datamodules you're interested in)
update #1: original version of reply produced list of every component on form , name of datasource/mastersource, if any. i've changed provide improve match inquire in body of q).
update #2: fixes couple of slips in update #1 version, , re-implements hasdatasource function in way avoids generating exception when examining components don't have 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 exit; aobject := getobjectprop(acomponent, pinfo); result := tdatasource(aobject); end; begin result := false; adatasource := getdatasource('datasource'); if adatasource <> nil result := true; if result exit; adatasource := getdatasource('mastersource'); if adatasource <> nil result := true; end; procedure tform1.log(msg: string); begin memo1.lines.add(msg); end; procedure tform1.finddatasourceobjects(acontainer : tcomponent); var : integer; adatasource : tdatasource; procedure logdatasourcename(acontainer : tcomponent); begin log(acontainer.name + ' datasource: ' + adatasource.name); end; begin if hasdatasource(acontainer, adatasource) logdatasourcename(acontainer); := 0 acontainer.componentcount - 1 begin finddatasourceobjects(acontainer.components[i]); end; end; procedure tform1.btnfindclick(sender: tobject); begin finddatasourceobjects(self); end; the dfm of form is
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 delphi delphi-7
Comments
Post a Comment