2
votes

Given the following test form (tested in Delphi 10.1 and Delphi 2007):

object Form1: TForm1
  Caption = 'Form1'
  ClientHeight = 340
  ClientWidth = 639
  object b_Test: TButton
    Caption = 'Test'
    OnClick = b_TestClick
  end
  object od_test: TOpenDialog
    Filter = 'blub (blub*.dbf)|blub*.dbf|bla (bla?.dbf)|bla?.dbf'
  end
end

(I left out some default properties, basically it's a form with a TOpenDialog and a button. The only important part is the TOpenDialog.Filter property.)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    od_test: TOpenDialog;
    b_Test: TButton;
    procedure b_TestClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.b_TestClick(Sender: TObject);
begin
   od_test.Execute;
end;

end.

Why does the dialog show the filter containing a question mark twice like this:

duplicate filter

Is this a known bug or am I doing anything wrong? If it's a known bug, is there a workaround (apart from the obvious one of leaving out the ' (bla?.dbf)' part of the description which would mean I had to change all descriptions.)?

EDIT: This is on Windows 8.1. I haven't tried other versions yet. And in case it makes a difference: I have disabled the explorer option "Hide extensions for knonw file types".

1
Cannot reproduce with Delphi 10.1 Berlin on Windows 8.1. Perhaps some OS issue?Uwe Raabe
I would expect the filter string to be: 'blub|blub*.dbf|bla|bla?.dbf'. Repeating the mask in the name is not necessary. So in your setup, the bla (...) (...) is actually correct, but blub should also have been blub (...) (...).Rudy Velthuis
@RudyVelthuis, if you want to see the filter mask in the description you have to write it. It won't be added automatically. The filter string is correct and the doubling shown in the picture is incorrect, although I cannot say where it comes from as I cannot reproduce it here.Uwe Raabe
OK, on Windows 10 I can reproduce it. If my filter string is 'blub (xxx)|blub*.dbf|bla (yyy)|bla?.dbf', then I get blub (xxx) (blub*.dbf) and bla (yyy) (bla?.dbf). But if I specify the same string as in the question, I get the same result. Apparently the open dialog scans the filter names for such () entries and if they are the same as the filter, they are only displayed once. It apparently recognizes *, but not ? as wildcard, so it does not remove the double entry for bla.Rudy Velthuis
@Uwe: If the filter is 'blub|blub*.dbf|bla|bla?.dbf' then the dialog shows 'blub (blub*.dbf)' and 'bla (bla?.dbf)', at least in 10.1 Berlin and Windows 10. Bit as far as I can remember, this has always been the case, and I have always defined fitlers like that. There is no need to repeat the mask in the filter name.Rudy Velthuis

1 Answers

6
votes

There are several factors here.

Factor 1

As I wrote in my comments, I usually use a simple filter, without repetition of the filter mask in the filter description:

OpenDialog1.Filter := 'blub|blub*.dbf|bla|bla?.dbf';

For me, that produces:

blub (blub*.dbf)
bla (bla?.dbf)

But when, in the Windows Explorer, I enable the folder option "hide extensions of known file types", the dropdown list of the combobox in the TOpenDialog looks like:

blub
bla

So now, the filter mask is not displayed anymore.

Factor 2

To avoid the omission of the filter mask, you can repeat it in the description, as in the question:

OpenDialog1.Filter := 'blub (blub*.dbf)|blub*.dbf|bla (bla?.dbf)|bla?.dbf';

If the folder option described above, under Factor 1, is disabled (IOW, if all extensions are shown), the open dialog apparently checks to see if the filter mask is included in the description, and then it doesn't display it in parentheses again. But this only works for filters that contain an asterisk, but, as it seems, not for filters that contain a question mark.

So that is why you get:

blub (blub*.dbf)
bla (bla?.dbf) (bla?.dbf)

The automatic display of the filter mask after the description was suppressed for blub, but not for bla.

Conclusion

So ISTM that you should either avoid filter masks with question marks, or if you really must, you should not include those in the description:

OpenDialog1.Filter := 'blub (blub*.dbf)|blub*.dbf|bla|bla?.dbf';

But this bears the risk that the filter mask is not displayed for the bla description at all, if your folder options hide extensions of known file types. So then, your combobox dropdown could look like this:

blub (blub*.dbf)
bla

I guess you'll have to decide. I don't know if or how you can query the folder option (that would be a nice new question). If you can, you can supply different filter strings, depending on the option.

Update

According to @dummzeuch, the info about the option can be found in the (surprise!) registry. Link: windowsitpro.com/systems-management/…

this can be changed/read from the registry at HKEY_CURRENT_USER\Software \Microsoft\Windows\CurrentVersion\Explorer\Advanced with the key HideFileExt. The logic is, of course reversed: Set to 0 to show all extensions, set to 1 to hide known extensions.


Personally,

I will keep on defining simple filters, like

OpenDialog1.Filter := 'blub|blub*.dbf|bla|bla?.dbf';

If people decided they don't want to see known extensions in their folders, they will not get the automatic display of the filter masks. If they decided to tell Windows they want to see all extensions, they will get the automatic display of the filter masks in the combobox.