Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show only certain file extensions in an open-file dialog?

When I open a folder with an OpenDialog, how can I filter it so users can view only certain files (e.g., Stringgrid, *.sg) and the files with any other extension do not appear in the dialog window?

like image 227
Andras Kelemen Avatar asked Nov 27 '11 23:11

Andras Kelemen


2 Answers

Set the OpenDialog.Filter property to the file filter you want.

You can do this in the Object Inspector:

  • Click in the Filter property, and you'll see a small button appear on the right edge with ....
  • Click that, and you'll see a dialog appear.

On the left side is the description of the file (for instance, Excel files (*.xls)). The right side is the filter you want to use, as in *.xls.

OpenDialog Filter Dialog

You can also set it in code before displaying your dialog:

OpenDialog1.Filter := 'Excel files (*.xls)|*.xls';

Of course, replace the Excel stuff with any description and mask you want to use.

like image 95
Ken White Avatar answered Sep 21 '22 07:09

Ken White


The Filter and FilterIndex properties are used to specify which file extension(s) to display (note that the user can override the filter manually, though).

The OnIncludeItem event is used to selectively allow/disallow individual files/folders from being listed in the dialog.

like image 35
Remy Lebeau Avatar answered Sep 22 '22 07:09

Remy Lebeau