【问题标题】:Why does the compiler say my form's variable is undeclared in my procedure?为什么编译器说我的表单变量在我的过程中未声明?
【发布时间】:2014-07-12 17:35:06
【问题描述】:

我想按照示例项目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. 

【问题讨论】:

    标签: delphi topendialog


    【解决方案1】:

    这是因为在您的实施部分,您的过程 ReadCSVFile 是独立的,而不是 TForm1 的方法(就像您的 SlctImportDtaClick 已经是)。改为阅读

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

    编译器抱怨的原因是,虽然 ReadCSVFile 被声明为独立过程,但它无法在其中的 MainOpenDialog 和声明为 TForm1 一部分的那个之间“建立连接”。

    【讨论】:

    • 非常感谢。我继续前进。
    • 如果您在表单上放置了 TOpenDialong,则无需创建或释放它。表单就是这样做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多