PiedSoftware 4 Posted Thursday at 10:30 PM Hi How can I get a list of all the data-aware controls whose datasource is one that is specified? I have googled and even consulted Claude, without success, but I thought there was a way. Any ideas? Share this post Link to post
Uwe Raabe 2132 Posted Thursday at 10:47 PM You can iterate the protected DataLinks property (there are several ways to access a protected property described elsewhere). For each TDataLink in this list act according to the actual class type: A TFieldDataLink gives you access to the corresponding Control, while a TGridDataLink provides the connected Grid. There are other classes inherited from TDataLink with other purposes, but that should you get started. Share this post Link to post
PiedSoftware 4 Posted Friday at 04:19 AM (edited) Thanks for the thought, Uwe. I should have said I'm using Delphi 10.4. I tried this function: uses Vcl.DBCtrls, System.Generics.Collections, System.Classes; type TLocalDataSource = class(TDataSource); function GetDataAwareList(ds: TDataSource): TArray<TControl>; var lds: TLocalDataSource absolute ds; links: TList<TDataLink>; link: TDataLink; fldLink: TFieldDataLink; cmp: TComponent; begin result := []; links := lds.DataLinks; for link in links do begin if link is TFieldDataLink then begin fldLink := link as TFieldDataLink; cmp := fldLink.Control; if cmp is TControl then begin result := result + [TControl(cmp)]; end; end; end; end{ GetDataAwareList}; It keeps seizing in the middle there, no particular line, sometimes reading fldlink.Control, sometimes when adding it to the array. I've been hacking at that for a while, stretching it out with more local variables to make it finer grained, but it has never worked for me. I'll just have to go ahead with code that is aware of what controls it needs to manage. EDIT: It seems that it does actually work. It was the debugger that was freezing. When I don't single step through it, it works fine. Edited Friday at 04:45 AM by PiedSoftware Share this post Link to post
PiedSoftware 4 Posted Monday at 05:50 AM Here is the code when reduced in size because it works: type TLocalDataSource = class(TDataSource); // Exposes protected property DataLinks function GetDataAwareList(ds: TDataSource): TArray<TControl>; var lds: TLocalDataSource absolute ds; link: TDataLink; fldLink: TFieldDataLink absolute link; begin result := []; for link in lds.DataLinks do begin // See, I told you if (link is TFieldDataLink) and (fldLink.Control is TControl) then begin result := result + [TControl(fldLink.Control)]; end; end; end{ GetDataAwareList}; Share this post Link to post