delphi – 如何找出哪些DB-Aware控件链接到TDataSource?

delphi – 如何找出哪些DB-Aware控件链接到TDataSource?,第1张

概述我有DataSource1(TDataSource),并且有一些链接到它的DB-Aware控件(通过SomeDBControl.DataSource = DataSource1) 如何在代码中找出(枚举)哪些控件链接到给定的TDataSource? 下面的代码使用RTTI,在D7中为我工作,通过递归搜索容器对象(即表单及其组件)列出具有DataSource或MasterSource属性的组件. ( 我有DataSource1(TDataSource),并且有一些链接到它的DB-Aware控件(通过SomeDBControl.DataSource = DataSource1)

如何在代码中找出(枚举)哪些控件链接到给定的TDataSource?

解决方法 下面的代码使用RTTI,在D7中为我工作,通过递归搜索容器对象(即表单及其组件)列出具有DataSource或MasterSource属性的组件.

(显然,您可以对您感兴趣的任何其他表单/数据模块执行类似 *** 作)

更新#1:此答案的原始版本生成了表单上每个组件的列表以及其DataSource / MasterSource的名称(如果有).我已经改变它以提供与你在q的身体中所要求的更好的匹配.

更新#2:这修复了Update#1版本中的一些漏洞,并以一种避免在检查没有DataSource / MasterSource属性的组件时生成异常的方式重新实现HasDataSource函数.

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是

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  endend
总结

以上是内存溢出为你收集整理的delphi – 如何找出哪些DB-Aware控件链接到TDataSource?全部内容,希望文章能够帮你解决delphi – 如何找出哪些DB-Aware控件链接到TDataSource?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1273479.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存