使用FindComponent递归查找控件

问题描述 投票:0回答:1

我动态创建了 Layout1。

object Layout1: TLayout
  Align = Client
  object Rect1: TRectangle
    Align = Client    
    object Button1: TButton
      Text = 'Test'
    end
  end
end

为什么当我使用 Layout1.FindComponent('Button1') 时,它返回 nil? (完全大写)

我以为FindComponent是递归的?

然后我尝试了

if Layout1.FindComponent('Rect1') <> nil then;  // this find Rect1

但只有当我设置时

Rect := Layout1.FindComponent('Rect1')

然后使用

if Rect.FindComponent('Button1') <> nil then  // only then it works

有什么建议吗?被这个

FindComponent
的东西搞糊涂了。应该递归的东西其实不是递归的。

还是只有.FindComponent是递归的,其他组件FindComponent都不递归?

delphi components
1个回答
0
投票

AComponent.FindComponent
查找
AComponent
Owner
的所有组件。因此,您必须将
FindComponent
用于创建该组件时使用的 Button1
owner
,而不是用于父层次结构中某处的控件。

© www.soinside.com 2019 - 2024. All rights reserved.