假设我有很多相同的控件如50个EDIT控件,
 控件名称分别为EDIT1,EDIT2,EDIT3,EDIT4....EDIT50
 现在我要给控件赋值,不想写50行赋值语句
 怎么用循环语句赋值呢
 如这样:
 for i:=1 to 50 do
  edit[i].text:=array[i];  
  //这句是错的,那位大侠知道如何写循环

解决方案 »

  1.   

    var
      j: integer;
    begin
      j :=0;
      for i:=0 to componentcount-1 do
      begin
        if components[i] is tedit then
        begin
          (components[i] as tedit).text = ary[i];
          j := j+1;
        end;
      end;   
    end;
      

  2.   

    for i := 0 to ComponentCount - 1 do
    begin
      if Components[i] is TEdit then
        TEdit(Components[i]).Text := Array[i];
    end;
      

  3.   

    var
      j: integer;
    begin
      j :=0;
      for i:=0 to componentcount-1 do
      begin
        if components[i] is tedit then
        begin
          (components[i] as tedit).text = ary[i];
          j := j+1;
        end;
      end;   
    end;
      

  4.   

    for iCount:=0 to ComponentCount - 1 do
      if Components[iCount] is TEdit then
          TEdit(Components[iCount]).Text:='';
      

  5.   

    var I:Integer;J代表數組,請注意Edit的次序即Components[I]值與array[J]的對應問題  for I := 0 to ComponentCount - 1 do
      begin
         IF ( Components[I] is TEdit ) then 
         begin
           (Components[I] as TEdit).Text := array[J];
           J:=J+1;
         end;
      end;
      

  6.   

    你可以使用主Form的Components数组+Is,As类型操作函数来实现类似的功能,
      

  7.   

    var I:integer;  for I := 0 to ComponentCount - 1 do
      begin
         IF ( Components[I] is TEdit ) then (Components[I] as TEdit).Text := array[J];
      end;
      

  8.   

    楼上的那位,你的代码可靠吗?
    我也是用你那种方法实现某个功能 ,
    但是如果有其它控件(尤其是无焦点)的干扰,
    就会报错的,好不容易搞个几个Try 才算搞掂,
    但,这理论确实是可行的。
      

  9.   

    应是:
    for i := 0 to ComponentCount - 1 do
    begin
      edit[i]:=Tedit.create(application);
       edit[i].Text := Array[i];
    edit[i].name:=intstr(i);
    edit[i].bouds:=??
    edit[i].true:=true;
      
    end;
      

  10.   

    我给你个,绝对有效,我调试过的,是cb中的,你改一下就可以了,给分哦
     TEdit *Edit
     for(int i=1; i<=componentcount; i++){
            Edit = dynamic_cast<TLabelEffect*>(
                FindComponent("Edit"+IntToStr(i)));
            if (Edit!= NULL)
             {Edit->Text.c_str=array[i];  
    }
      

  11.   

    DELPHI 控件数组的简易实现:
    在DELPHI的每一个VCL组件中都有个属性:COPONENTINDEX;利用这个就实现控件数组:
    PROCEDUER TFORM1.BUTTON1(SENDER:TOBJECT);
    VAR INDEX:INTEGER
    BEGIN
      FOR INDEX:=0 TO CONTROLCOUNT-1 DO
      BEGIN
        IF COMPONENTS[INDEX] IS TEDIT THEN
           BEGIN
              (COMPONENTS[INDEX] AS TEDIT).COLOR := C1LIME; 
           END;
      END;
    END;PROCEDUER TFORM1.EDIT1ENTER(SENDER:TOBJECT);
    BEGIN
      //得焦点时改背景为YELLOW
      (SENDER AS TEDIT).COLOR := C1YELLOW;
    END;PROCEDUER TFORM1.EDIT1EXIT(SENDER:TOBJECT);
    BEGIN
      //失焦点时改背景为白
      (SENDER AS TEDIT).COLOR := C1WHITE;
    END;-------------------------------------
      

  12.   

    for i:=0 to Panel1.ControlCount-1 do
    begin
      if Panel1.Controls[i].ClassNameIs('TEdit') then
      TEdit(Panel1.Controls[i]).Text:=...;
    end;