I am using a TOpenDialog to allow the user to open 3 different types of files. When showing the initial directory it lists only the files that match the current extension type chosen which is what I want. However, when the user selects one of the other types the filenames that match that type never show up. If I type *.ext in the filename box then all of the filenames of that extension show. How can I get that result when the user changes the filetype?
Asked
Active
Viewed 533 times
1 Answers
1
If a wildcard mask has been typed in to the edit box, the dialog only displays files that match that mask, regardless of what file type is selected in the drop-down list. The mask is not updated automatically with a new file extension when the user selects a different file type from the list. You have to do that manually in the OnTypeChange
event, eg:
uses
..., Dlgs, Commdlg;
procedure TForm1.OpenDialog1TypeChange(Sender: TObject);
const
Extensions : array[1..4] of PChar = ('', 'ext1', 'ext2', 'ext3');
var
s : String;
i : Integer;
begin
OpenDialog1.DefaultExt := Extensions[OpenDialog1.FilterIndex];
if OpenDialog1.FilterIndex = 1 then Exit;
i := SendMessage(GetParent(OpenDialog1.Handle), CDM_GETSPEC, 0, 0);
if i > 1 then
begin
SetLength(s, i-1);
SendMessage(GetParent(OpenDialog1.Handle), CDM_GETSPEC, i, LPARAM(PChar(s)));
s := ChangeFileExt(s, '.' + Extensions[OpenDialog1.FilterIndex]);
SendMessage(GetParent(OpenDialog1.Handle), CDM_SETCONTROLTEXT, edt1, LPARAM(PChar(s)));
end;
end;

Remy Lebeau
- 555,201
- 31
- 458
- 770
-
Thanks. However, delphi doesn't recognize the string CDM_GETSPEC or CDM_SETCONTROLTEXT constants. Where are they located? – user1429254 Jun 01 '12 at 05:19
-
What is edt1. Thanks for the help. – user1429254 Jun 01 '12 at 16:11
-
`edt1` is a Windows-defined ID number that represents the filename edit control of the dialog. It is defined in the `Dlgs` unit. See the "Explorer-Style Control Identifiers" section of http://msdn.microsoft.com/en-us/library/windows/desktop/ms646960.aspx. – Remy Lebeau Jun 01 '12 at 18:05
-
When the dialog box opens the proper files types are shown. However, if I change the filetype the main filelist box looks like it updates but no files of the selected type are shown. If I change the type back to the first one the proper files show up. When tracing the procedure i = 0 because no filename has been typed at this point. I have tried adding an else statement below: begin s := '*.' + Extensions[OpenDialog.FilterIndex]; SendMessage(GetParent(OpenDialog.Handle), CDM_SETCONTROLTEXT, lst1, LPARAM(PChar(s))); end; changing the lst1 to edt1 without any difference – user1429254 Jun 01 '12 at 20:35
-
`i` will be 0 if the user has not typed in a filename yet. The purpose of the code is to just change only the file extension portion of whatever filename the user has typed in. You have to use `edt1` when sending `CDM_SETCONTROLTEXT` in order to update the filename edit box. `lst1` is for the file listbox instead, which you cannot assign a filename to. – Remy Lebeau Jun 01 '12 at 21:58
-
I tweaked the code in my answer. This code works fine for me now. – Remy Lebeau Jun 01 '12 at 22:15
-
The dialog is for opening files. I only want to show in the list box only those files that match the desired filter and any folders. For example, if the current folder has files: 1,2,3 as jpg files, 4,5,6 as mov files, and 7,8,9 as txt files and the file filter is jpg only the files 1,2,3 will appear. If the filter is changed to mov then only 4,5,6 will appear. The same thing for txt. I do not want to change the extension of the file but only want to see certain files based on the type of filter. – user1429254 Jun 02 '12 at 15:18
-
1What you describe is exactly how the dialog natively operates without you having to do anything extra. If it is not doing that for you, then you have to be doing something in your code to prevent it. The extra coding I've shown is just to keep the user-entered filename in sync with the selected filter as it changes, nothing else. The code has no effect on what the listbox displays, the filter does that. Makes me start to suspect your filter is not configured properly to begin with. Can you please show your actual filter data? – Remy Lebeau Jun 02 '12 at 22:30
-
OpenDialog.Filter := 'Measured Pipe Length Files( *.MPL)| *.MPL|Log Files(\*.log)| *.LOG | TVD Logs ( *.tvd) | *.TVD'; – user1429254 Jun 03 '12 at 04:35
-
1@user - Your filter is wrong, remove the spaces from file extensions for it to work as you want (better remove all unnecessary spaces). – Sertac Akyuz Jun 03 '12 at 09:32
-
I had to add the spaces to get it to post correctly. I will make sure there are no spaces in the extensions. Thanks. It seems the problem was the space after LOG and before the |. Thanks again. – user1429254 Jun 03 '12 at 20:48