我需要制作'图形用户界面'我需要一些VCL组件来选择一些文件。
此组件必须选择文件,但用户不必输入文件名。
我正在搜索信息但没有任何帮助我。
答案 0 :(得分:9)
Vcl.Dialogs.TOpenDialog可用于此目的。
另见UsingDialogs。
procedure TForm1.Button1Click(Sender: TObject);
var
selectedFile: string;
dlg: TOpenDialog;
begin
selectedFile := '';
dlg := TOpenDialog.Create(nil);
try
dlg.InitialDir := 'C:\';
dlg.Filter := 'All files (*.*)|*.*';
if dlg.Execute(Handle) then
selectedFile := dlg.FileName;
finally
dlg.Free;
end;
if selectedFile <> '' then
<your code here to handle the selected file>
end;
请注意,此处的示例假定TButton
名为Button1
的内容已删除到表单,而TForm1.Button1Click(Sender: TObject)
过程已分配给按钮OnClick
事件。
TOpenDialog.Filter
属性可以使用|
(管道)字符将它们连接在一起,可以使用多个文件扩展名:
'AutoCAD drawing|*.dwg|Drawing Exchange Format|*.dxf'
答案 1 :(得分:0)
我发现了我的问题。 我的问题是我没有按任何按钮来打开Dialog。 我做了一个TEdit。这个Tedit有一个类似的程序(Onlclik):
procedure TForm1.SelectFile(Sender: TObject);
begin
openDialog := TOpenDialog.Create(self);
openDialog.InitialDir := 'C:\';
openDialog.Options := [ofFileMustExist];
// Allow only .dpr and .pas files to be selected
openDialog.Filter :=
'All files (*.*)|*.*';
// Display the open file dialog
if openDialog.Execute
then ShowMessage('File : '+openDialog.FileName)
else ShowMessage('Open file was cancelled');
// Free up the dialog
openDialog.Free;
end;