请问各位大虾DELPHI中怎么在程序中遍历一个窗体上面的全部控件?

解决方案 »

  1.   

    for i:=0 to Form1.ComponentCount-1 do
    begin  Form1.Components[i]......end;
      

  2.   

    同意楼上的。
    procedure TForm1.Button2Click(Sender: TObject);
    var
     i:Integer;
    begin
      for i:=1 ComponentCount-1 do
      begin
        Memo1.Lines.Add(Components[i].Name);
      end;
    end;
      

  3.   

    for i:=0 to ComponentCount-1 do
     Showmessage(Components[i].Classname); 
      

  4.   

    for i:=1 to Form1.ControlCount do
    Begin
        Form1.Controls[i].xxxxxx
    End;
      

  5.   

    i:Integer;
    begin
      for i:=1 ComponentCount-1 do
      begin
        ...Components[i]....;
      end;
    Components是vcl的全局因子,让你可以遍历vcl组件
    ComponentCount是当前vcl组件的个数。
      

  6.   

    如果用了以上各位朋友的办法并且如果窗口里有PAGECONTROL,PANEL之类的容器控件,并且这些容器控件上面了放了很多的EDIT BUTTON控件那遍历到PAGECONTROL,PANEL这类容器控件后,就不会再遍历进去,不会把容器控件里的EDIT,BUTTON都遍历出来
      

  7.   

    hntjc():
      不会的,设计时放的vcl元件都以Form为Owner
      所以同意My_first(海浪)的
      

  8.   

    =======================================
    这里找不到答案吗?来这里试试看!
    这里有问必答
    http://systemer.51.net/cgi-bin/leoboard.cgi
    希望你能在这里找到你满意的答案
    =========================================
      

  9.   

    怎么不会,我遇到这个问题不知道有多久了你不信自己简单试了,窗口里放一只PANEL,在PANEL里放一只EDIT,BUTTON然后用  楼上的办法循环遍历看,肯定遍历到PANEL就停止了EDIT,BUTTON遍历不到,如果PANEL不放,EDIT,BUTTON就能遍历的到了
      

  10.   

    呵呵,俺说两句:显然不是所有的VCL组件都以Form1为Owner。要证明这一点,可以右键点击Form1,选择“View as text”,看看组件之间的创建顺序和父子关系就明白了。要列出Form1中所有的孩子组件,就象查找指定文件夹中的所有文件(包括子文件夹)一样,是一个多重查找。楼上已经有关键的查找方法了,俺就不罗嗦了。
      

  11.   

    打开对象浏览器观察form的结构,会发现是一棵树,你就把它当作一棵树来遍历就可以了。
    做一个递归就可以了
    unit test
    interfaceprocedure DigIntoContainer(Container: TComponent);implementationvar
       Components: TStrings;procedure DigIntoContainer(Container: TComponent)
    //只有component的子类才可能出现在面板上可视化设计
    var
      I: Integer;
    begin
      if not Assigned(Components) then Exit;
      Components.Add(Container.Name);
      if Container.ComponentCount = 0 then Exit;
      for I := 1 Container.ComponentCount - 1 do
      begin
       if Container.Components[I].ComponentCount = 0 then
         Component.Add(Container.Components[i].Name)
       else
       begin
         DigIntoContainer(Container.Components[i]);
       end;
      end;  
    end;initialization
      Components := TStringList.Create;finalization
      FreeAndNil(Components);
      

  12.   

    hntjc()
    我试了,都能找到,你说的应该是Controls吧
    unit Unit5;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TMainForm = class(TForm)
        Button2: TButton;
        Panel1: TPanel;
        Edit1: TEdit;
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.Button2Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to ComponentCount - 1 do
        ListBox1.Items.Add(Components[I].Name);
    end;end.
    dfm.object MainForm: TMainForm
      Left = 192
      Top = 107
      Width = 544
      Height = 375
      Caption = 'Form'
      Color = clBtnFace
      Font.Charset = ANSI_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = 'Arial'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 15
      object Button2: TButton
        Left = 272
        Top = 272
        Width = 75
        Height = 25
        Caption = 'Button2'
        TabOrder = 0
        OnClick = Button2Click
      end
      object Panel1: TPanel
        Left = 8
        Top = 24
        Width = 225
        Height = 233
        Caption = 'Panel1'
        TabOrder = 1
        object Edit1: TEdit
          Left = 40
          Top = 24
          Width = 121
          Height = 23
          TabOrder = 0
          Text = 'Edit1'
        end
        object Button1: TButton
          Left = 48
          Top = 184
          Width = 75
          Height = 25
          Caption = 'Button1'
          TabOrder = 1
        end
        object ListBox1: TListBox
          Left = 40
          Top = 64
          Width = 121
          Height = 97
          ItemHeight = 15
          TabOrder = 2
        end
      end
    end
      

  13.   

    上面的递进关系只是说明谁是谁的parent关系,
    并不说明owner
      

  14.   

    这是指设计时放上去的控件是以Form为owner;
    如果运行时创建的控件指定Owner为别的东西的话就找不到了
      

  15.   

    老达摩,难道你没注意自己贴出来的这段代码了?
    object Panel1: TPanel
        Left = 8
        Top = 24
        Width = 225
        Height = 233
        Caption = 'Panel1'
        TabOrder = 1
        object Edit1: TEdit
          Left = 40
          Top = 24
          Width = 121
          Height = 23
          TabOrder = 0
          Text = 'Edit1'
        end
        
    很明显EDIT1的OWER是PANEL,那用你的FOR NEXT的方法根本遍历不到EDIT1了,只能遍历到PANEL了
      

  16.   

    自己试一下同意  xzgyb(老达摩)
      

  17.   

    hntjc():
     试试就知道了另外可以做个实验unit TestComponent;interfaceuses
      Windows, Messages, SysUtils, Controls, Classes;type
      TTestControl = class(TControl)
      private
        { Private declarations }
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
      published
        { Published declarations }
      end;procedure Register;implementation
    uses Dialogs;procedure Register;
    begin
      RegisterComponents('GybCtrl', [TTestControl]);
    end;{ TTestComponent }constructor TTestControl.Create(AOwner: TComponent);
    begin
      inherited;
      ShowMessage(AOwner.Name);
    end;end.
    随便弄个控件放在窗体上,无论放在Form上,或放在Panel上
    弹出来的都是窗体名称的对话框,也就是说Owner都是窗体
      

  18.   

    我刚试了,顶楼说的没问题,panel内的一样可以遍历到