unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);  private
    { Private declarations }
  public    { Public declarations }  end;var
  Form1: TForm1;implementation{$R *.dfm}
 function readFromFile:string;
var s,s2:string; F:TextFile;
begin
  OpenDialog1.FileName:='';
  OpenDialog1.Filter:='*.XML|*.xml';
  s:='';  result:='';
  if not OpenDialog1.Execute then exit;  assignfile(F,OpenDialog1.FileName);
  try
    Reset(F);
    while not Eof(F) do
    begin
      Readln(F, s2);
      s:=s+s2;
    end;
    result:=s;
  finally
    closefile(f);
  end;
  end;运行就提示: Undeclared identifier: 'OpenDialog1',帮我看看,哪儿问题

解决方案 »

  1.   

    with Form1 do
    begin......end;
      

  2.   

    或者把方法声明为
     function TForm1.readFromFile:string;
      

  3.   

    function readFromFile:string你这样定义函数就是说此函数不在类中,但你又要调用这个类TFORM中的对象,所以报错,解决办法就是把这个函数加到类里,就可以了
      

  4.   

    函数中所有控件前加上窗体
    如:form1.OpenDialog1
      

  5.   

    先在private里面定义函数,然后再执行。
      

  6.   

    private
      { Private declarations }
    //这加上
    function readFromFile:string;//这改为
    $R *.dfm}
     function TFrom1.readFromFile:string;
    var s,s2:string; F:TextFile;
    begin...
      

  7.   

    自己创建opendialg 不直接拖控件。function readFromFile:string;
    var
       s,s2:string;
       F:TextFile;
       OpenDialog1: TOpenDialog;
    begin
      OpenDialog1 := TOpenDialog.Create(nil);
      try
        OpenDialog1.FileName:='';
        OpenDialog1.Filter:='*.XML|*.xml';
        s:=''; result:='';
        if not OpenDialog1.Execute then exit;
        assignfile(F,OpenDialog1.FileName);
        try
          Reset(F);
          while not Eof(F) do
          begin
            Readln(F, s2);
            s:=s+s2;
          end;
          result:=s;
        finally
          closefile(f);
        end;
      finally
        OpenDialog1.free;
      end;
    end;