FindComponent在程序中不起作用

Alb*_*ola 3 delphi lazarus

我正在开发一个程序来计算不同数据的平均值,TStringGrid我想用一个程序.它被称为calcola.

procedure calcola(numero:ShortInt; StringGrid:TStringGrid; pbarprog:ShortInt);
var i,j,cont,num:shortint;
    avg,temp,numfl:double;
    a:string;
    Edit1:TEdit;
begin
if StringGrid.Colcount>1 then

 //other code

       avg:=temp/cont;
       TLabel(FindComponent('Label'+IntToStr(num))).Caption:=FloatToStrF(avg, ffGeneral, 1, 1);
       Edit1.Text:=FloatToStr(StrToFloat(TLabel(FindComponent('Label'+IntToStr(num))).Caption)*10);
       TProgressBar(FindComponent('ProgressBar'+IntToStr(i+pbarprog))).Position:=StrToInt(Edit1.Text);

       //other code

     end;
   end;
end; 
Run Code Online (Sandbox Code Playgroud)

在这个过程中,Lazarus告诉我" Identifier not found FindComponent ".然后我剪切/粘贴相同的代码procedure TForm1.Button1Click(Sender: TObject);,我没有错误.

我需要在FindComponent()里面使用calcola,我怎么能这样做?

Mar*_*ynA 6

然后我在程序TForm1.Button1Click(Sender:TObject)中剪切/粘贴相同的代码; 我没有错.

编译器在进行更改时停止抱怨的原因是当calcola被声明为TForm1的方法时,编译器可以通过向后搜索TForm1的声明方法以及它所来自的对象的公共方法来解析标识符FindComponent( TForm ... TObject)直到找到一个用该名称声明的.FindComponent在TComponent中声明.

编译器抱怨你的原始版本的原因是calcola在程序的全局范围内被声明(我假设)作为一个独立的例程,对于那些,编译器只搜索先前声明的独立过程/函数,而不是那些被声明为对象的方法.

如果由于某种原因你的calcola程序绝对必须是一个独立的程序,那么最好的办法是调整它的参数,这样你就可以将TForm1的特定实例作为参数传递给它,就像你使用StringGrid一样.