我定义了一个public的 txtlb:array [1..10] of TLabel的数组,然后在一个function里面付它caption不同的值,而我那个function返回的是要整型,现在调用那个function没有问题,可是现在我在一个button的click事件里面想显示出它所有caption的值,但是发现全部为空,没有带出来,不知道怎样解决!
特此求救,大伙们帮帮忙^_^,急阿!

解决方案 »

  1.   

    function staticload(query: TADOQuery ; lb: TLabel ; examPan: TScrollBox ; txtlb: array of TLabel; anslb: array of TLabel; sel: array of TRadioGroup; selecRad: TRadioGroup;stansload: array of string; index:integer; title:TLabel):integer;
    var
            content: array [0..100] of string;
            ans: array [0..100] of string;
            i,length,amount,j: integer;
            step_x,step_y,seperator:integer;
            AnswerFile:TextFile;
    begin       title:=TLabel.Create(lb);
           title.Parent:=examPan;
           title.Caption:='一.单选题(共40题,每题1.5分)';
           title.Font.Style := [fsBold];       step_x := 10;
           title.SetBounds(10,10,title.Width,title.Height);
           step_y := 10+title.Height;
           seperator := 30;       AssignFile(AnswerFile,'standardAns.goc');
           ReWrite(AnswerFile);
           j:=index*40+1;
           for i:=Low(TMyArr) to High(TMyArr) do
           begin
           query.SQL.Clear;
           query.SQL.Add('select * from exam where ID='+inttostr(j));       try
            query.Open;
            if not query.Eof then
            begin
                        content[i] := query.FieldbyName('Content').AsString;
    //                    stansload[i] := query.FieldByName('standardans').AsSTring;
                        Writeln(AnswerFile,query.FieldByName('standardans').AsSTring);
                        ans[i] := query.FieldbyName('Selection').AsString;
                        txtlb[i] := TLabel.Create(lb);
                        anslb[i] := TLabel.Create(lb);
                        //selection part initial
                        sel[i] := TRadioGroup.Create(selecRad);
                        sel[i].Width := 150;
                        sel[i].Height := 30;
                        txtlb[i].Parent := examPan;
                        anslb[i].Parent := examPan;
                        sel[i].Parent := examPan;
                        txtlb[i].Caption := inttostr(i)+'.  '+content[i];
                        txtlb[i].AutoSize := false;
                        anslb[i].Caption := ans[i];
                        sel[i].Columns := 4;
                        sel[i].Items.Clear;
                        sel[i].Items.Add('A');
                        sel[i].Items.Add('B');
                        sel[i].Items.Add('C');
                        sel[i].Items.Add('D');
                        sel[i].Tag := i;
                        sel[i].OnClick := MainF.Button1.OnClick;                    //setbounds
                        txtlb[i].SetBounds(step_x,step_y,txtlb[i].Width,txtlb[i].Height);
                        anslb[i].SetBounds(step_x+15,step_y+txtlb[i].Height,anslb[i].Width,anslb[i].Height);
                        sel[i].SetBounds(step_x+15,step_y+txtlb[i].Height+anslb[i].Height+20,sel[i].Width,sel[i].Height);
                        step_y := step_y+txtlb[i].Height+anslb[i].Height+sel[i].Height+10+20;
                        inc(j);
              end
              else
              begin
                    showmessage('题库为空或不全');
                    break;
              end;       except
              Showmessage('connect fail');
           end;
           query.Close;
           end;
           CloseFile(AnswerFile);
              staticload:=step_y;end;
      

  2.   

    就是在这个function里面我付了txtlb的值,然后在另外一个函数里面调用txtlb的caption,发现全部为nil,值没有带出来好像,我的txtlb是public的
      

  3.   

    看不大明白,设置txtlb[i].name属性试试?
      

  4.   

    函数声明中txtlb: array of TLabel; 改为
    var  txtlb: array of TLabel;
    试试。
      

  5.   

    是不是要这个?
      public
        { Public declarations }
        txtlb:array [1..4] of TLabel;
        procedure settxt;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      SetTxt;
      showmessage(TxtLb[1].Caption);
    end;procedure TForm1.settxt;
    begin
      TxtLb[1]:=TLabel.Create(self);
      TxtLb[1].Caption:='11';
      TxtLb[1].Parent:=Form1;
      TxtLb[2]:=TLabel.Create(Self);
      TxtLb[2].Caption:='22';
      TxtLb[2].Parent:=Form1;
    end;