Ahmed Faizan
Ahmed Faizan

Reputation: 456

Cannot filter pdfs and txt files in OpenFileDialog

I have been using this filter to filter pdfs and other files.

 ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf |Office Files|*.doc;*.xls;*.ppt |Txt files (*.txt); *.txt | ";

It had been working well for a long time. However, I can only filter Word files now. Why can't I filter any other type of file now?

Upvotes: 0

Views: 2330

Answers (2)

Caius Jard
Caius Jard

Reputation: 74615

I'd have written:

 ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf|Office files (*doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt|Text files (*.txt)|*.txt";

Ive no idea explanation as to why it was working before; what you have in your question doesn't follow the rules laid out in the documentation (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.filter?view=netframework-4.7.2)

The basic rule is:

description1|extensionlist1|description2|extensionlist2...

Multiple extensions delimited by semicolon. Descriptions can contain any text and don't have to contain the extension list

Upvotes: 1

Bradley Smith
Bradley Smith

Reputation: 13601

That's not a valid filter string; you're missing a pipe symbol after the Txt files entry, and there's an extraneous pipe at the end. It should look like this:

ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf|Office Files|*.doc;*.xls;*.ppt|Txt files (*.txt)|*.txt";

Upvotes: 3

Related Questions