2005. március 5., szombat

How to get a list of all data-aware controls linked to a given TDataSource


Problem/Question/Abstract:

Is there a way to get a list of data-aware controls linked to a given TDataSource component? I need a solution that will work without knowing ahead of time what form to look in for the controls.

Answer:

Try something like the following. This code will scan all components on all forms and determine if the component has a DataSource property. If it does, the value of the DataSource property is assigned to the variable ThisDataSource.

for I := 0 to Screen.FormCount - 1 do
  if Screen.Forms[I] is TCustomForm then
    with Screen.Forms[I] as TCustomForm do
      for J := 0 to ComponentCount - 1 do
        if IsPublishedProp(Components[J], 'DataSource') then
        begin
          ThisDataSource := GetObjectProp(Components[J], 'DataSource') as TDataSource;
          if ThisDataSource = SomeOtherDataSource then
            {...}
        end;

Nincsenek megjegyzések:

Megjegyzés küldése