Board index » cppbuilder » OpenDialog1->InitialDir not working

OpenDialog1->InitialDir not working


2003-08-18 10:48:53 PM
cppbuilder10
The InitialDir property of the TOpenDialog and TSaveDialog classes
doesn't seem to be working properly for me. The variable
"OpenDialog1->InitalDir" is being assigned to what I want it to be, but
the dialog box opens up with the previously used folder. Allow me to
further explain. The following constructor successfully obtains the
working directory, where the executable is run and saves it in a global
character array called workDir. Clicking Button1 the first time does
start in the current directory, but if I move to a different folder,
then click this Button1 again, the OpenDialog immediately defaults to
the previous folder, not the workDir folder, as if the
"OpenDialog1->InitialDir = workDir" has no effect. Debugging does show
workDir to be correct. I read in the Help that a boolean variable
called "ForceCurrentDirectory" may need to be set to true. I tried that
and still no help. Maybe it's the Windows 2000 operating system. Any
ideas?
Thanks,
Mike D.
#include <dir.h>
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
strcpy(workDir, "X:\\"); // workDir is a global char array
workDir[0] = 'A' + getdisk(); // replace X with current drive letter
getcurdir(0,workDir+3); // fill rest of string with current dir
}
void __fastcall TForm1::Button1Click(TObject *Sender) {
char tempWorkDir[255];
OpenDialog1->InitialDir = workDir;
if (OpenDialog1->Execute())
{
// load file ...
}
else { return; }
}
 
 

Re:OpenDialog1->InitialDir not working

Mike Disbrow,
Firstly this isn't the right place to be asking this question. The
IDE newsgroup section deals with issues arising from the IDE (Editor,
De{*word*81} etc) and this rarely involves programming issues.
I suggest you repost to a more appropriate section
(.VCL.COMPONENTS.USING would probably be most appropriate since
TOpenDialog is a VCL component) but include a fully compilable
example that can demonstrate the problem.
You need the example because many of us have used TOpenDialog without
any such problems and as it stands your message seems impossible.
Quote
I read in the Help that a boolean variable
called "ForceCurrentDirectory" may need to be set to true.

In my opinion it is bad practice to do anything with the current
working directory. Life is usually simpler if all paths are held
internally as fully qualified. You can store them (in a config or
project file) as relative but programmatically it is better to work
with fully qualified paths as it eliminates an area of uncertainty.
The Borland IDE is itself an example of what kind of mess you can get
into if you don't follow this rule. See my bug #720. It can also
prevent 'Go to declaration working' if you have opened a file and
changed the CWD.
To this end we always ensure that dialogs are set not to change the
working directory.
We always set the option 'ofNoChangeDir' about which the help says:
"After the user clicks OK, resets the current directory to whatever
it was before the file-selection dialog opened."
Oh and if you want to obtain the location of the EXE just can use
GetModuleFileName().
Andrue Cope
[Bicester, UK]
 

Re:OpenDialog1->InitialDir not working

Thanks for the shortcut in getting the working directory, but my problem
still remains that the open/save dialog box does NOT start in the
working directory (i.e. the line "OpenDialog1->InitialDir" does not work
like it's supposed to). I'm thinking it may be my operating system
(Windows 2000). Have you tried this code on your computer? The first
time you click the button, the dialog may show the correct default
folder, but if you change folders and select a file in a different
folder, then click the button again, it remembers the last folder you
were in and does not default to what "InitialDir" was told.
JD wrote:
Quote
Mike Disbrow < XXXX@XXXXX.COM >wrote:
>The InitialDir property of the TOpenDialog and TSaveDialog
>classes doesn't seem to be working properly for me. [...]

String workDir;

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
workDir = ExtractFilePath( Application->ExeName );
}
//----------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OpenDialog1->FileName = "";
OpenDialog1->InitialDir = workDir;
if( OpenDialog1->Execute() )
{
// load file ...
}
}
//------------------------------

~ JD
 

{smallsort}