3

I want to be able to open files in Delphi with a Windows GUI where you can scroll through the folders etc. I have already done this with Matlab with a single function that (after selecting the file) returns a string of the path. You could event specify which extension the be shown. Is this kind of function available in delphi and how should I use it.

Jort
  • 79
  • 1
  • 7
  • possible duplicate of [What is the difference between the new TFileOpenDialog and the old TOpenDialog?](http://stackoverflow.com/questions/6236275/what-is-the-difference-between-the-new-tfileopendialog-and-the-old-topendialog) – Lieven Keersmaekers Sep 13 '11 at 13:47
  • @Lieven: I don't think the OP even knows that there are different looking open dialogs. :-) – Uli Gerhardt Sep 13 '11 at 13:53
  • @Ulrich - true, the title of that question isn't a good fit (at all) but if OP looks at the answers given, it is all there. I was a bit in lingo about this myself... Too late now anyway, I can't undo the *close* vote. – Lieven Keersmaekers Sep 13 '11 at 14:01
  • @Lieven, the word you wanted was *limbo* (undecided), not *lingo* (vocabulary), but it's funny because if Jort had known the lingo (for instance, that they're called *dialog boxes*, and therefore that Delphi's Dialogs component group might be a good place to look), this question might not have needed to be asked. – Rob Kennedy Sep 13 '11 at 16:22
  • @Rob - so I did some good after all :) – Lieven Keersmaekers Sep 13 '11 at 18:02

2 Answers2

7

you can use the TOpenDialog component which is part of the Dialogs unit. you can create in runtime or drop this component from the Dialogs palette.

if you drop the component to your form you can use in this way

 OpenDialog1.Filter := 'Only Text files (*.txt)|*.txt';
 if OpenDialog1.Execute then
  //do you stuff here

or if you create the component in runtime

Var
  OpenDialog1 : TOpenDialog;
begin
 OpenDialog1:=TOpenDialog.Create(nil);
 try
   OpenDialog1.Filter := 'Only Text files (*.txt)|*.txt';
   if OpenDialog1.Execute then
    ShowMessage('Selected File '+OpenDialog1.FileName);
 finally
   OpenDialog1.Free;
 end;

end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Thanks, but I copied these lines under a button but nothing happens when I hit it. I have the Dialogs statement in my uses. And how do I get the name (and dir) use in the rest of the code? – Jort Sep 13 '11 at 14:06
  • @Jort It works fine for the rest of us. Unless you give us more details we can't help. Don't make us guess what your code looks like. – David Heffernan Sep 13 '11 at 14:12
  • The code works if you drop the component to the form first. Anyway I updated the answer adding an sample creating the component in runtime too. btw to get name of the selected file you must use the `FileName` property . – RRUZ Sep 13 '11 at 14:14
  • @Pruz, Thank you for the extra information.. Now I see how it works. – Jort Sep 13 '11 at 14:17
  • @Jort not problem, FYI is `RRUZ` not Pruz :) – RRUZ Sep 13 '11 at 14:19
  • 4
    @ To whomever approved az01's modification - My opinion is that, getting rid of the local variable in behalf of a `with`, is **not** making the code *cleaner*. – Sertac Akyuz Sep 13 '11 at 17:09
  • 2
    I Just rollback the modifications, please don't edit the code adding `with`. – RRUZ Sep 13 '11 at 17:21
1

That's available via TOpenDialog which encapsulates the relevant Windows functionality.

Drop a TOpenDialog component on your form. Then you can call OpenDialog1.Execute to show the Windows dialog.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83