How to pop-up "Save As" Dialog with "Edit Browse Control"

Takaki KAMEYAMA 20 Reputation points
2023-03-15T06:39:39.03+00:00

I am creating dialog-based program with MFC and C++ in VisualStudio 2022.

I dragged & dropped "Edit Browse Control" to the dialog in the dialog editor. After run the program, when I clicked it, "Open" dialog pop-up as a default.

However I don't know how to let it pop-up as "Save As" dialog. I would like to know it.

I used CFileDialog in the past, but it is obsolete and Toolbox in VisualStudio 2022's dialog editor doesn't have CFileDialog Control. I would like to use Edit Browse Control if possible.

Thank you,

Developer technologies C++
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2023-03-15T08:01:04.6733333+00:00

    Hi Takaki KAMEYAMA,

    I suggest you still use CFileDialog, it's not obsolete.

    Although it cannot be dragged out directly in Toolbox, you could still create it using code.

    Sample Code:

    void CMFCApplication1Dlg::OnBnClickedButton1()
    {
    	// TODO: Add your control notification handler code here
    	TCHAR szFilters[] = _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");
    
    	// Create an Open dialog; the default file name extension is ".my".
    	CFileDialog fileDlg(FALSE, _T("my"), _T("*.my"),
    		OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
    
    	// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
    	// returns IDOK.
    	if (fileDlg.DoModal() == IDOK)
    	{
    		CString pathName = fileDlg.GetPathName();
    
    		// Implement opening and reading file in here.
    
    		//Change the window's title to the opened file's title.
    		CString fileName = fileDlg.GetFileTitle();
    
    		SetWindowText(fileName);
    	}
    }
    

    Result:

    User's image

    Regarding CMFCEditBrowseCtrl this documentation states that when the user clicks the browse button, the control performs a custom action or displays a standard dialog box that contains a file browser or a folder browser.

    In most cases it is only used for browsing files.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 49,536 Reputation points
    2023-03-15T09:11:58.19+00:00

    As @YujianYao-MSFT has commented the CFileDialog class is not obsolete. In fact, it is used internally by the CMFCEditBrowseCtrl class for the file browser.

    However, the CMFCEditBrowseCtrl class is hard-coded to create an "Open" dialog and is designed to function accordingly. You could derive your own class from CMFCEditBrowseCtrl and override the virtual OnBrowse() method where the file browser is instantiated to suit your needs. There could be additional changes needed to accommodate changing the functioning of the derived class from "Open" to "Save As" but I leave that task to you.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.