【发布时间】: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