locked
OpenFileDialog filter doesn't work RRS feed

  • Question

  • Pls check http://msdn.microsoft.com/en-us/library/w5tyztk9.aspx example.

    I changed ofn.filter like below.

    ofn.filter = "Log files\0*.log\0Batch files\0*.bat\0DOC\0*.doc\0DOCX\0*.docx\0";

    But it doesn't refresh files in the folder when I chnage file types combo in OpenFileName dialog box.

    I have to hit ENTER key in Filename text box to show files in the folder.

    Why it doesn't refresh? Pls some1 help.

    Wednesday, October 17, 2012 9:46 AM

Answers

All replies

  • Filter is only meant for one type of files.

    if you want for more, you have to do some additional method, that will look over all extensions.


    Mitja

    Wednesday, October 17, 2012 10:09 AM
  • Hi,

    I guess you're using the OpenFileDialog class. Its filter property is set up slightly different: "txt files (*.txt)|*.txt|All files (*.*)|*.*"

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Wednesday, October 17, 2012 10:10 AM
  • Doesn't solve my problem. I changed like this also.

    ofn.filter = "All files (*.*)\0*.*\0Txt files (*.txt)\0*.txt\0DOC (*.doc)\0*.doc\0DOCX (*.docx)\0*.docx";

    It doesn't refresh files in the dialog box when I chnage All ot Txt, Txt to Doc, Doc to Docx from the windows combo. I have to press ENTER key to view files after selecting the file type.

    Wednesday, October 17, 2012 10:26 AM
  • Hi,

    If you look closely, you will see pipes are used, not backslashes.

    Change your filter to "All files (*.*)|*.*|Txt files (*.txt)|*.txt|DOC files (*.doc)|*.doc|DOCX files (*.docx)|*.docx"

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Wednesday, October 17, 2012 10:32 AM
  • Please check, your filter doesn't work in this example. There the separator is \0 not the | sign.

    Wednesday, October 17, 2012 11:20 AM
  • Hi,

    I have checked, have you?

    Take a look at the OpenFileDialog class, and more specifically the Filter property.

    From MSDN:
    For each filtering option, the filter string contains a description of the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options are separated by the vertical bar.

    The following is an example of a filter string:

    Text files (*.txt)|*.txt|All files (*.*)|*.*

    You can add several filter patterns to a filter by separating the file types with semicolons, for example:

    Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*

    Use the FilterIndex property to set which filtering option is shown first to the user.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Wednesday, October 17, 2012 11:48 AM
  • I guess you are talking about System.Windows.Forms.OpenFileDialog

    I'm talking about GetOpenFileName of comdlg32.dll

    • Proposed as answer by Jason Dot Wang Friday, October 19, 2012 7:57 AM
    Wednesday, October 17, 2012 12:05 PM
  • If you're using GetOpenFileName, try using the equivalent of ChrW(0) as the separator, not just \0.

    To use this from C#, reference Microsoft.VisualBasic and use Microsoft.VisualBasic.Strings.ChrW(0). this would make your filter as follows:

    char separator;
    
    separator = Microsoft.VisualBasic.Strings.ChrW(0);
    ofn.filter = "Log files" + separator + "*.log" + separator + "Batch files" + separator + "*.bat" + separator + "DOC" + separator + "*.doc" + separator + "DOCX" + separator + "*.docx";

    Hope this helps.


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Wednesday, October 17, 2012 12:17 PM
  • use this line as filter 

    ofn.filter = "Log files\0*.log\0Batch files\0*.bat\0Doc files\0*.doc\0DocX files\0*.docx\0";

    you are missing the "files" keyword after the type DOC and DOCX.

    "Log files\0*.log\0Batch files\0*.bat\0DOC\0*.doc\0DOCX\0*.docx\0";

    regards

    joon



    • Edited by Joon84 Wednesday, October 17, 2012 12:22 PM
    • Proposed as answer by Jason Dot Wang Friday, October 19, 2012 7:58 AM
    • Marked as answer by Jason Dot Wang Thursday, October 25, 2012 8:08 AM
    Wednesday, October 17, 2012 12:21 PM
  • Thanx Joon, It's working. My next problem is I use ofn.flags = 0x10000000

    But it doesn't show hidden files in the folder. What may be the problem.

    Thursday, October 18, 2012 6:41 AM
  • check this link, http://stackoverflow.com/questions/2514523/api-to-toggle-show-hidden-files-folders-and-drives. I guess they have the same situation like you.

    regards

    joon

    • Marked as answer by Jason Dot Wang Thursday, October 25, 2012 8:08 AM
    Friday, October 19, 2012 8:04 AM
  • Hi DevNC,

      Welcome to MSDN Forum Support.

      If you feel Joon84's suggestion meets your requirement, please mark it as answers. And you need to post a new thread though you want to ask any more.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 8:06 AM