FindComponent 和 FindChildControl 理解.东摘西抄的,结合 owner 和 parent 看

FindComponent是TComponent的方法,TComponent有FComponents,用来管理控件,你用FindComponent就需要FComponents的支持,FComponents怎么来的呢?他是一个TList,自然需要你创建然后insert了,对于Tpanel,如果没有动态创建的控件,你用FindComponent有什么意义呢  
  FComponents里面应该是空的,就算是TButton也应该找不到的!  
  所以如果用FindComponent,就只能用from,不能用panel.

  Panel1->Components表示被Panel1拥有(Owner)的组件,而我们拖放组件到Panel1上时(比如:TButton),TButton的Owner其实默认为TForm,所以Panel1->ComponentCount还是为零;如果我们动态生成TButton   *btn1=TButton(Panel1);这时Panel1->ComponentCount才有意义。  
   
      我们拖放组件到Panel1上时,只是说明组件的Parent(TControl类型)为Panel1   ,所以通过Panel1->Controls可以查到Parent为Panel1的组件。

Button隶属于:TObject->TPersistent->TComponent->TControl->TWinControl->TButtonControl  
  Image隶属于:TObject->TPersistent->TComponent->TControl->TGraphicControl  
  SpeedButton隶属于:TObject->TPersistent->TComponent->TControl->TGraphicControl  
   
  按照C++Builder的规定,TWinControl是指那些在运行期可见的组件,而TGraphicControl属于运行期可见的图形组件,尽管具有相同的祖先,但两者属于不同的类.

FindComponent是TComponent的方法,TComponent有FComponents,用来管理控件,你用FindComponent就需要FComponents的支持,FComponents怎么来的呢?他是一个TList,自然需要你创建然后insert了,对于Tpanel,如果没有动态创建的控件,你用FindComponent有什么意义呢  
  FComponents里面应该是空的,就算是TButton也应该找不到的!  
  所以如果用FindComponent,就只能用from,不能用panel.

      if   FindChildControl('button1')<>nil   then  
          AButton:=FindChildControl('button1')   as   TButton;   //用as强制  

FindComponent,是Owner,如果你的Shape是设计期放上去的,那么其Owner就是窗体,用窗体的FindComponent肯定可以找到。

如果你的Shape是运行期创建的,那么必须指定其Owner和Parent,这样,才可以找得到。

而且:如果你放上去的是TWinControl的继承类,那么用FindChildControl就可以找到,否则就找不到。

转载于:https://www.cnblogs.com/yxbsmx/archive/2008/09/30/1302502.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值