CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Posts
    119

    CFileDialog dynamically change filter list

    Is there any way to dynamically update the filter list (i.e. "Save as type") once a CFileDialog is already created?

    Here is a brief run down of what I'm trying to do. Our program uses a profile system which allows you to select from a bunch of options and then save them as a file type which you can then select from the save dialog. Right now I accomplish this by simply building the filter list using the profiles before the save dialog is created, which is easy. However my boss has just requested that I add feature which allows users to create a new profile directly from within the save dialog. I have got it all working except that I can't figure out a way to add the newly created profile to the CFileDialogs filter list. Our save dialog is using an derived class with an overlay template for additional UI elements, so I have almost full control over it. But I can't seem to figure out any way to add file types to the filter list dynamically. Does anyone know how to accomplish this?

    Thanks,
    Dan

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CFileDialog dynamically change filter list

    Have you tried modifying m_ofn.lpstrFilter?

  3. #3
    Join Date
    Mar 2004
    Posts
    119

    Re: CFileDialog dynamically change filter list

    That's what I do now before the dialog is displayed with DoModal. However I need to update the list after the dialog is already being displayed. There may be some message I can send which reparses the m_ofn.lpstrFilter string, but if there is I don't know what it is.

    Dan

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: CFileDialog dynamically change filter list

    You can pick the idea from this Codeguru article: File Open and Save As Dialogs for MFC Applications Using GDI+.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Mar 2004
    Posts
    119

    Re: CFileDialog dynamically change filter list

    He's doing the same thing I am right now, which is construct the filter list in the DoModal function. What I want to do is change the filter list after the dialog is already being displayed. (i.e. after DoModal)

    Dan

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: CFileDialog dynamically change filter list

    Did you try to directly modify the content of cmb1 combobox of the CFileDialog template?
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2004
    Posts
    119

    Re: CFileDialog dynamically change filter list

    I did, and I can add text to it. However if I actually select the new item the program crashes. Presumably there is something stored in the ItemData or ItemDataPtr the dialog needs, but I'm not sure what it is.

    Dan

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: CFileDialog dynamically change filter list

    Well, then why not to check what data other combobox items have?
    Another guess: perhaps, all combo items must correspond to m_ofn.lpstrFilter string entries?
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2004
    Posts
    119

    Re: CFileDialog dynamically change filter list

    I tested the other combo items and found that the ItemData is set to a BSTR with "*.ext", where ext is the file extension. So basically I can do this...

    CComboBox *pWndFilesOfTypeCombo = (CComboBox*)GetParent()->GetDlgItem(cmb1);

    int index = pWndFilesOfTypeCombo->AddString(_T("Filter (*.ext)"));

    CString filter("*.ext");
    pWndFilesOfTypeCombo->SetItemData(index, (DWORD)filter.AllocSysString());

    And it seems to work.

    Thanks for helping me in the right direction.

    Dan

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: CFileDialog dynamically change filter list

    You are welcome!
    And thank you for the useful information about "ItemData set to a BSTR ..."!
    Victor Nijegorodov

  11. #11
    Join Date
    Mar 2009
    Posts
    29

    Re: CFileDialog dynamically change filter list

    Good day,

    I have a similar problem.

    I have a CFileDialog used to "Save As..." and i want to get a pointer to the "file_extension"ComboBox to see what the user selected by calling pComboBox->GetCurSel.

    How is this accomplished?

    Thanx in advance,
    stakon


    PS.
    I know i can get the selected Extension with the various get methods, but i want to have multiple times the same extension in my CFileDialog.

    EDIT: I used the code above and it is working well. I hadn't realized that "cmb1" is actually a defined variable name in file "dlgs.h"
    Last edited by stakon; September 25th, 2009 at 04:10 AM.

  12. #12
    Join Date
    Mar 2004
    Posts
    119

    Re: CFileDialog dynamically change filter list

    You could use something like this...

    CComboBox *pWndFilesOfTypeCombo = (CComboBox*)GetParent()->GetDlgItem(cmb1);

    int index = pWndFilesOfTypeCombo->GetCurSel();

    CString ext;
    ext.SetSysString((BSTR)pWndFilesOfTypeCombo->GetItemData(index));

    That will get you the index of the selected item and the extension of that selection.

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured