请教大虾一个问题:在一个窗体中,放了100个Label.它们的Caption都设为空了.
而在数据表Lists中,有50条记录.如何循环的将Lists表中的字段Names的值赋到Label的Caption中?
在线等..谢了...

解决方案 »

  1.   


    补充:基于Access数据库.有没示例代码?
      

  2.   

    使用TDataSet控件TdataSet.open;
    Tdataset.first;
    while not Tdataset.eof do begin
      lable[i].caption := Tdataset.Fields('names').asstring;
    end;
    TdataSet.close; 
      

  3.   


    我是楼主,为什么我的代码不行?procedure TLinksForm.CreateLinks;
    var
        i,j:Integer;
        tmpComp:TComponent;
    begin
        if TxtPar<>'' then
        begin
            with DataM.Q2 do
            begin
                Close;
                SQL.Clear;
                SQL.Add('Select sName from Sites where spar='''+TxtPar+''' ');
                Open;
                if RecordCount>0 then
                begin
                    for J:= 0 to RecNo do
                    begin
                        //初始化超链接
                        for I := 0 to ComponentCount - 1 do
                        begin
                            if J=I then
                            begin                    
                              tmpComp := Components[I];
                              if Not (tmpComp Is TLabel) then Continue;
                              TLabel(tmpComp).Caption:=VarToStr(FieldValues['sName']);
                            end;
                        end;
                    end;                                
                end;            
            end;         
        end;
    end;
      

  4.   

    procedure TLinksForm.CreateLinks; 
    var
      TxtPar: String;
      i: Integer;
    begin
    //  TxtPar := 'TxtPar';
      ADOConnection1.Connected := True;
      with ADOQuery1 do begin
        SQL.Text := 'SELECT sName FROM Sites WHERE spar=''' + TxtPar + '''';
        try
          Open;
          if RecordCount < 0 then Exit;
          for i := 0 to self.ComponentCount - 1 do begin  //这里要用 self,因为前面用了 with 关键字
            if self.Components[i] is TLabel then begin
              TLabel(self.Components[i]).Caption := FieldValues['sName'];
              Next;
            end;
          end;
        finally
          Close;
        end;
      end;
    end;
      

  5.   

    TLabel(FindComponent('Label1')).Caption:='hello world';
      

  6.   

    Open后要记行把ADOQuery1记录移到第一条,就是ADOQuery1.first
      

  7.   

    楼主的For循环应该是 0 to RecordCount - 1,而不是 0 to RecNo
      

  8.   

    procedure   TLinksForm.CreateLinks;   
    var 
        TxtPar:   String; 
        i:   Integer; 
    begin 
    //     TxtPar   :=   'TxtPar'; 
        ADOConnection1.Connected   :=   True; 
        with   ADOQuery1   do   begin 
            SQL.Text   :=   'SELECT   sName   FROM   Sites   WHERE   spar='''   +   TxtPar   +   ''''; 
            try 
                Open; 
                if   RecordCount   <   0   then   Exit; 
                for   i   :=   0   to   self.ComponentCount   -   1   do   begin     //这里要用   self,因为前面用了   with   关键字 
                    if   self.Components[i]   is   TLabel   then   begin 
                        TLabel(self.Components[i]).Caption   :=   FieldValues['sName']; 
                        Next; 
                    end; 
                end; 
            finally 
                Close; 
            end; 
        end; 
    end;