Reputation: 123
I need to make 'Graphic User Interface' and I need some VCL component to select some file.
This component have to select the file, but the user don't have to put the name of the file.
I am searching information but nothing helps me.
Upvotes: 4
Views: 23358
Reputation: 4868
Vcl.Dialogs.TOpenDialog can be used for this purpose.
See also 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;
Notice that the example here assumes that a TButton
named Button1
is dropped to the form and the TForm1.Button1Click(Sender: TObject)
procedure is assigned to the button OnClick
event.
Multiple file extensions can be used in the TOpenDialog.Filter
property by concatenating them together using the |
(pipe) character like this:
'AutoCAD drawing|*.dwg|Drawing Exchange Format|*.dxf'
Upvotes: 19
Reputation: 123
I FIND my problem. My problem was that i was not making any button to open the Dialog. I make a TEdit. This Tedit have a procedure (Onlclik) like that:
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;
Upvotes: -1