1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Preselecting files in a CFileDialog

Discussion in 'MFC' started by ill_comms, Feb 16, 2011.

  1. ill_comms

    ill_comms New Member

    Just wondering how and if you can pre-select multiple files as the dialog opens? Do you place a list of them in the Initial Filename area, as I've tried this already the answer seems to be, No.
    Am I missing something?

    Basically I'd like a user to be able to select multiple files, I then do some checking of the filenames and if one of these checks comes back false, I want to reload this dialog with the files that the user had previously selected.

    Any help would be greatly appreciated
    Hayden

    Code:
    CFileDialog dlg(FALSE/*Open=TRUE Save=False*/,NULL/*Filename Extension*/,""/*Initial Filename*/,OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT/*Flags*/,"All Files(*.*)|*.*||"/*Filetype Filter*/);
    	
    	const int nBufferSize = 128*1024; // May be excessive?
    	TCHAR *szNewBuffer = new TCHAR[nBufferSize];
    	memset(szNewBuffer, 0, sizeof(TCHAR) * nBufferSize);
    	dlg.m_ofn.lpstrFile = szNewBuffer;
    	dlg.m_ofn.nMaxFile = nBufferSize - 1;
    	
    	if (dlg.DoModal() == IDOK)
    	{
    		POSITION pos = dlg.GetStartPosition();
    		CString fullpath;
    		CString filenametemp;
    		CString filename;
    		int nLastSlashPos;
    
    		filePaths = "";
    		fileNameList = "";
    		
    		while (pos)
    		{
    			fullpath = dlg.GetNextPathName(pos);
    
    			nLastSlashPos = fullpath.ReverseFind('\\');
    			if(nLastSlashPos >= 0)
    			{
    				filename = fullpath.Mid(nLastSlashPos+1);
    			}
    
    			if (filePaths != "")
    			{
    				filePaths = filePaths + "|";
    				fileNameList = fileNameList + " ";
    			}
    			filePaths = filePaths + fullpath;
    			fileNameList = fileNameList + '"' + filename + '"';
    		}
    	}
     

Share This Page