Search code examples
c++filteropenfiledialog

OpenFileDialog->Filter in windows form application in C++ does not work


in my windows form project, in C++ , visual 2010, I'm using this line with this purpose that user just select this types of files:

openFileDialog1->Filter =  "Image Files(*.png, *.jpg)|*.png;*.jpg";

this line is written before openfiledialog1->ShowDialog();

but when I run the project and open the OpenFileDialog, I can see files in other format, such as *.txt.

where is my fault?

thank you so much ...


Solution

  • The documentation for what you are trying to do is here: https://learn.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea

    The string needs to be formatted using nulls as delimiters for the different sections of a filter. So, your string should be formatted as follows:

    openFileDialog1->Filter =  "Image Files (*.png, *.jpg)\0*.png;*.jpg\0";