We help IT Professionals succeed at work.

Changing location in Open/Save dialog

Yechezkel
Yechezkel asked
on
How can I programatically change the current location in an open/save dialog while it is open (from within a class derived from CFileDialog)? I can put the new path in the edit box which shows the filename, but how can I get the list control to go there?
Comment
Watch Question

Commented:
The CFileDialog has a data member m_ofn, it is a window OPENFILENAME structure.
You can set m_ofn.lpstrInitialDir to the path you want.

for example, you may do like this:

     CFileDialog fd(true);

     fd.m_ofn.lpstrInitialDir = "c:\\";

     if(fd.DoModal() == IDOK)
     {
     //     Do something;
     }

then you will open the file dialog in C:\ dir.

Author

Commented:
If you read my question clearly you will see that I want to change the location _while the dialog is open_.
Meir RivkinFull stack Software Engineer

Commented:
if u wann change the location while the dialog is open what is the problem ?
Author of the Year 2009
Commented:
Just put some text (a directory name) into the File editbox and programmatically click OK:

void CMyFileDlg::AnyFn()
{
    CWnd* pRealDlg= GetParent();
    CWnd* pEditFile= pRealDlg->GetDlgItem(0x480);

    pEditFile->SetWindowText("c:\\temp");

    pRealDlg->PostMessage(WM_COMMAND, IDOK,0);
}

If you are worried about that 0x480 value, you can #include a file named .../include/dlgs.h that has the IDs but they are not much help.  For instance, the ID of the Filename box is edt1

-- Dan

Author

Commented:
The problem is that that closes the dialog. I want the location to change while the dialog stays open.

Commented:
You can use the DlgDirList() function (of CWnd class). It fills a list box with a file or directory listing. Only thing you should know is the ID of the list control. The declaration is:

int DlgDirList( LPTSTR lpPathSpec, int nIDListBox, int nIDStaticPath, UINT nFileType );

Commented:
Sorry, I didn't read your question carefully.
DanRollins is right, actually he is click "Open" button.
But in dialog template the "Open" button has the same ID as "OK" button. it is 0x0001.
And in fileopen dialog, if you click "open" and the text in FileName is a disrectory, you will change to that dir, The dialog will not close.

Author

Commented:
That's not exactly what I want...

Author

Commented:
(The previous comment was for arun_ta)

Author

Commented:
Thanks! I was trying it with the full filename. And Thanks j_jin for clarifying it for me.
BTW SendMessage works instead of PostMessage which allows me to set the edit box to the filename (without the path) immediately after showing the directory.
Author of the Year 2009

Commented:
Test carefully before putting SendMessage in your production code.  

I avoided it because My test code was running as a notification handler -- a typical usage for this would be to change directories when the user changes file types; that is in the OnTypeChange() function.

In some cases (perhaps not this one) you can get into trouble taking major actions in a notification handler. The SendMessage could trigger a cascade of actions... one of which is another notification.  Just a thought.

-- Dan

P.S. Thanks for your fuss-free accepting of my answer.  Often when someone like arun_ta locks a question, it can be a hassle straightening things out.