I want to use CFileDialog class to open files. To do this, I wrote the short
code below :
-------------------------------
CFileDialog openFileDialog (TRUE, _T (".xyz"), 0, 4 | 2, _T ("XYZ
Files(*.xyz)|*.xyz All Files(*.*)|*.*||"), this, 0, 0);
openFileDialog.m_ofn.Flags = openFileDialog.m_ofn.Flags | OFN_EXPLORER;
if (IDOK == openFileDialog.DoModal ())
{
txtBpsFileName.SetWindowTextW (openFileDialog.GetFileName ());
CString cStr = openFileDialog.GetFolderPath ();
}
-----------------------------
With GetFileName (), I have no problem but after GetFolderPath () is called
an assertion fails stating m_hWnd of the CFileDialog is NULL. I added
OFN_EXPLORER style but nothing changed. Can you help me if you know the
case?
Thanks in advance.
>Hi,
>
>I want to use CFileDialog class to open files. To do this, I wrote the short
>code below :
>
>-------------------------------
>
>CFileDialog openFileDialog (TRUE, _T (".xyz"), 0, 4 | 2, _T ("XYZ
>Files(*.xyz)|*.xyz All Files(*.*)|*.*||"), this, 0, 0);
****
What does 4 | 2 mean? This cannot possibly make any sense whatsoever!!!! These are
flags, and if you meant to specify OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, then that's
what you must write! The value 4 | 2 is nonsensical. Why didn't you just write 6? It is
shorter, and equally nonsensical. Code like this is illegible, error-prone, and
unmaintainable.
It is Best Practice to put that filter string in your STRINGTABLE. Not every language
uses the words "Files" or "All"
****
>
>openFileDialog.m_ofn.Flags = openFileDialog.m_ofn.Flags | OFN_EXPLORER;
****
So why did you not add OFN_EXPLORER to the flags when you had the constructor for
CFileDialog?
If you feel you have to do this as a separate operation, why did you not write
openFileDialog.m_ofn.Flags |= OFN_EXPLORER;
???
****
>
> if (IDOK == openFileDialog.DoModal ())
> {
> txtBpsFileName.SetWindowTextW (openFileDialog.GetFileName ());
***
You should have said SetWindowText and *not* SetWindowTextW (this is a pervasive defect of
Intellisense; it goes for the current raw API call, not the macro version of the call,
which is, to put it mildly, remarkably stupid)
****
> CString cStr = openFileDialog.GetFolderPath ();
****
There is an addition in the latest documentation, which was omitted from earlier
documentation:
"You can call this method only while the dialog box is being displayed. After the dialog
box has been closed, this function will no longer work, and the method will fail with an
assertion"
If you don't have the latest documentation installed, it is always a good idea to check
the MS Web site for the very latest documentation. You should state the version of VS
you are using so we can check to see if the documentation is consistent with what you are
doing.
In general, when relating assertion calls, it is ESSENTIAL to provide ALL the relevant
information, not some vague handwave about some message. You need to say which version of
VS you are using, the file and line reported in the assertion message,
joe
****
> }
>
>-----------------------------
>
>With GetFileName (), I have no problem but after GetFolderPath () is called
>an assertion fails stating m_hWnd of the CFileDialog is NULL. I added
>OFN_EXPLORER style but nothing changed. Can you help me if you know the
>case?
>
>Thanks in advance.
>
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm