执行
 ShowMessage(IntToStr(Self.Panel1.ComponentCount));
显示0;
执行
 if ( Self.Panel1.Components[0] is TLabel ) then
   ShowMessage(TLabel(Self.Panel1.Components[0]).caption);
报错:
 List index out of bounds (0)
为什么?

解决方案 »

  1.   

    List index out of bounds (0)
    出错表示你的Panel1不拥有子组件,
    Componentcount 表示父组件拥有子组件的数目,即是子组件的 Owner 是 父组件,
    你放在Panel1上的组件的 Owner 是 Form,(Panel1的Owner也是Form),Parent 是 Panel1;
    for i:=0 to self.componentcount -1 do
    begin
    if (UpperCase( Self.Components[i].Parent.Name)=UpperCase('Panel1') and
         Self.Components[i] is TLabel )then
      ShowMessage(TLabel(Self.Panel1.Components[i]).caption);
    end;
      

  2.   

    ComponentCount 依赖于 Owner
    ControlCount 依赖于 Parent如动态创建组件:
     lblTest := TLabel.Create(form1);
    则 lblTest: Ower = Form1
      lblTest: Parent = nil
    要手动设置: lblTest.Parent := Form1;
    -------------------------------------
    如果自动创建(也就是你直接从控件板放到FORM上或PANEL上):
     则Owner = form
     Parent=form (如果放在FORM上)
     parent=panel(如果放在Panel上)