【问题标题】:Delphi, openDialog, no window appears on clickDelphi,openDialog,点击时不出现窗口
【发布时间】:2014-07-03 14:02:24
【问题描述】:

我目前正在尝试稍微修改this tutorial 将 Excel 文件加载到 Delphi 中。我想使用 OpenDialog 来获取文件路径并启动后续程序,将文件加载到文本框并启动连接程序。我起草了下面的代码,但注意到在编译文件并单击按钮后会发生。我的理解是单击按钮应该显示打开的文件窗口。我不明白为什么没有显示文件选择窗口。

procedure TForm1.Button1Click(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
  strConn : WideString; // Declare wide string for the connection

begin
  // Create the open dialog object - assign to our open dialog variable
  openDialog := TOpenDialog.Create(self);

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

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

  // Allow only .Excel and .pas files to be selected
  openDialog.Filter :=
    'Excel 2003|*.xls|Excel 2007 and newer|*.xlsx';

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

  // Give file path to the edit
  Edit1.Text := openDialog.FileName;


  // Connect the Excel file
  AdoConnection1.Connected:=False;
  AdoConnection1.ConnectionString:=strConn;

  end;

【问题讨论】:

  • 您的previous question(以及您收到的两个答案)中如何没有涵盖这一点?如果您不只是复制和粘贴代码,而是实际阅读并尝试理解代码及其在做什么,那么在遵循教程时会有所帮助。
  • 我认为您需要退后一步,花时间尝试理解代码,而不是仅仅将其粘贴到编辑器中并运行它。
  • 您还应该在使用完对话框后释放对话框(或者,可能重复使用单个对话框)。

标签: delphi topendialog


【解决方案1】:

您未能显示对话框。这样做:

if not openDialog.Execute then
  Abort;

显然,您需要在初始化属性之后,但在读取文件名之前执行此操作。

您之前显然已经成功地做到了这一点,从this earlier question 可以看出。正如我之前所说,将选择文件名的代码与其余代码分开是一个非常好的主意。尝试安排代码部分执行一项任务,并且单独执行一项任务。这样做可以使这些代码段可组合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多