小弟的问题如下:
    比如说有两个字符串 S := 'ABC'和 SR := 'FILE';
    请问如何遍历访问窗口中所有的可视控件的Name属性,如果该控件的Name属性等于 S,那么设置他的Caption := SR;
    请问该怎么做?大家一定帮忙啊,很急!!!

解决方案 »

  1.   

    访问Components数组例如TEdit类
    for i:=0 to componentcount-1 do
    id components[i] is TEdit then ...不过用Tag参数都是可以了
      

  2.   

    :)
    写错了一点
    for i:=0 to componentcount-1 do
    if components[i].Name = .. then ...
      

  3.   

    var i:integer;
    begin
      for i:=0 to self.ComponentCount-1 do
      begin
        if self.Components[i] is tlabel then 
          begin
            if Tlabel(self.Components[i]).name=s then
               begin
                 Tlabel(self.Components[i]).caption=sr
               end;
         end;
    end;
      

  4.   

    TControlEx = class(TControl);
    published
      property Text;
    end;var
      Component : TCoontrol;
    begin
      Component := TControl(FindComponent(S));
      if Component <> nil then TControlEx(Component).Text := SR;
    end;
      

  5.   

    for i:=0 to componentcount-1 do
     begin
      if Components[i] is TEdit then ...
      if Components[i].Name = .. then ...
     end;
      

  6.   

    根本不可能,Component[I] 根本没有 Caption 属性,而且,上面的好几段代码都可以编译!!!var i:integer;
    begin
      for i:=0 to self.ComponentCount-1 do
      begin
        if self.Components[i] is TLabel then 
          begin
            if Tlabel(self.Components[i]).name=s then
               begin
                 Tlabel(self.Components[i]).caption=sr
               end;
         end;
    end;上面的 TLabel 会提示没有声明。
      

  7.   

    上面的 TLabel 会提示没有声明
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~你uses TLabel了吗?
      

  8.   

    找 ‘可视控件’应该用ControlCount吧,
    from delphi help 
    TControl is the base class for all components that are visible at runtime.下面是实现:
     for i:=0 to self.ControlCount-1 do begin
         if self.Controls[i].Name =S then
          self.Controls[i].Name :=SR //对timer等非可视化控件,在此设置断点,根本得不到运行
     end;
      

  9.   

    晕了
    不过 self.Controls[i].Name :=SR 也可以改变 self.Controls[i]的caption的
    嘻嘻,投机取巧了所以正确答案(不是最好,但最简单,不过只能是符合控件名称的字符串,汉字就不行了)是:
     for i:=0 to self.ControlCount-1 do begin
         if self.Controls[i].Name =S then
          self.Controls[i].Name :=SR ;
     end;
      

  10.   

    如果不知道控件类型,可用如下方式
      for i:=0 to Self.ControlCount-1 do
        if (Self.Controls[i] is TWinControl)and(Self.Controls[i].Name ='S') then
          SetWindowText((Self.Controls[i] as TWinControl).Handle,'SR');
      

  11.   

    上面的 TLabel 会提示没有声明
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~你uses TLabel了吗?
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~说错了,应该是uses StdCtrls
      

  12.   

    用Controls[J]可以访问到了,但是菜单项目为什么不行啊?
    各位大哥帮帮忙啊
      

  13.   

    菜单项不是Control,必须单独遍历,而且是递归形式的,因为每个菜单项都有可能包含子菜单
      

  14.   

    同意  maozefa(之源) ,必须分开遍历。
    在win95中, user.exe使用两个32-bit heaps。其中一个用来储存WND 结构。另一個32-bit heap 用来存放menu。
    可见wnd与menu是两种孑然不同的东西.
    Nt没研究过
      

  15.   

    if form1.findcomponents('S') <> nil  then 
       ....