为什么编译器说我的表单的变量在我的程序中未声明?

时间:2014-07-12 17:35:06

标签: delphi topendialog

我想按照示例项目here中的建议,将一个CSV文件读取到Delphi(DBGrid)。我有一个简单的表单,我在其中定义了TOpenDialog和 TCsvTransform 中的元素。当我尝试创建一个将文件路径从TOpenDialog传递给负责读取CSV文件的过程的过程时,项目不会编译。

procedure ReadCSVFile;
var
  SS: TStringStream;
  OS: TFileStream;
begin
  OS := TFileStream.Create(MainOpenDialog.FileName, fmCreate);
  SS := TStringStream.Create;
  try
    ClientDataSet1.SaveToStream(SS, dfXML);
    with TCsvTransform.Create do
    try
      Transform(DPToCsv, SS, TStream(OS));
    finally
      Free;
    end;

  finally
    SS.Free;
    OS.Free;
  end;
end;

编译器说 MainOpenDialog 未声明。完整的代码,我认为我在下面宣布了Open Dialog元素。

unit geoimp;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.Buttons, Vcl.StdCtrls,
  Vcl.Grids, Vcl.DBGrids, Data.DB, Datasnap.DBClient, ShlObj;


 const
  shfolder = 'ShFolder.dll';

type
  TMainForm = class(TForm)
    MainPageControl: TPageControl;
    ImportTab: TTabSheet;
    MapPreviewTab: TTabSheet;
    GeoMatchingTab: TTabSheet;
    ImportDBGrid: TDBGrid;
    ImportLbl: TLabel;
    SlctImportDta: TSpeedButton;
    MainClientData: TClientDataSet;
    MainDataSource: TDataSource;
    MainOpenDialog: TOpenDialog;
    procedure SlctImportDtaClick(Sender: TObject);
    procedure ReadCSVFile;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure ReadCSVFile;
var
  SS: TStringStream;
  OS: TFileStream;
begin
  OS := TFileStream.Create(MainOpenDialog.FileName, fmCreate);
  SS := TStringStream.Create;
  try
    ClientDataSet1.SaveToStream(SS, dfXML);
    with TCsvTransform.Create do
    try
      Transform(DPToCsv, SS, TStream(OS));
    finally
      Free;
    end;

  finally
    SS.Free;
    OS.Free;
  end;
end;

procedure TMainForm.SlctImportDtaClick(Sender: TObject);
begin
  // Create the open dialog object - assign to our open dialog variable
  MainOpenDialog := TOpenDialog.Create(self);

  // Set up the starting directory to be the current one
  MainOpenDialog.InitialDir := GetCurrentDir;

  // Only allow existing files to be selected
  MainOpenDialog.Options := [ofFileMustExist];

  // Allow only .dpr and .pas files to be selected
  MainOpenDialog.Filter :=
    'CSV Files|*.csv';

  // Select pascal files as the starting filter type
  MainOpenDialog.FilterIndex := 2;

  // Display the open file dialog
  if MainOpenDialog.Execute
  then ShowMessage('File : '+MainOpenDialog.FileName)
  else ShowMessage('Open file was cancelled');

  // Free up the dialog
  MainOpenDialog.Free;
end;

end. 

1 个答案:

答案 0 :(得分:3)

那是因为在你的实现部分,你的程序ReadCSVFile是独立的,而不是TForm1的方法(就像你的SlctImportDtaClick一样)。将其更改为阅读

procedure TForm1.ReadCSVFile;
var
  SS: TStringStream;
  OS: TFileStream;
begin
  OS := TFileStream.Create(MainOpenDialog.FileName, fmCreate);
  [etc]

编译器抱怨的原因是,当ReadCSVFile被声明为一个独立的过程时,它无法建立连接"在它之间的MainOpenDialog和声明为TForm1的一部分之间。