Reputation: 229
I'm having a problem getting my OpenDialog window to start in the appointed initialdir. What I currently have is tihs
procedure TForm1.fileMenuLoadClick(Sender: TObject);
begin
SetCurrentDir(StartDir);
SetCurrentDir('Cases');
OpenDialog.Filename := '';
OpenDialog.InitialDir := GetCurrentDir;
OpenDialog.Filter := 'Sparfiler (.dat)|*.dat';
// -------------------------------
if OpenDialog.Execute then
begin
GeometryClear;
DerobModel.Filename := OpenDialog.Filename;
DerobModel.Open;
pressed := True;
SetCurrentDir('../');
DerobModel.HouseProperties.StringValue['CaseDir'] := GetCurrentDir;
DerobModel.HouseProperties.StringValue['StartDir'] := StartDir;
SetCurrentDir(StartDir);
UpdateGeometryPanel;
mainUpdateComboBox;
UpdatePropertiesPanel;
UpdateEnergyPanel;
UpdateAbsorption;
UpdateClimatePanel;
UpdateClimate;
mainHide;
Geometry.IsSelected := True;
GeometryPanel.Visible := True;
TreeView1.Enabled := True;
TreeView1.HitTest := True;
DerobModel.HouseProperties.BoolValue['GlazeChange'] := False;
end;
When running this code it keeps opening the folder of the last file I've opened. I read that the solution is to clear the FileName property of OpenDialog but it doesn't work. However the funny thing is that it is working with this code which is from a previous version of my app.
procedure TForm1.fileMenuLoadClick(Sender: TObject);
begin
SetCurrentDir(StartDir);
SetCurrentDir('Cases');
OpenDialog.Filename := '';
OpenDialog.InitialDir := GetCurrentDir;
OpenDialog.Filter := 'Sparfiler (.dat)|*.dat';
// -------------------------------
if OpenDialog.Execute then
begin
GeometryClear;
DerobModel.Filename := OpenDialog.Filename;
DerobModel.Open;
pressed := True;
SetCurrentDir('../');
DerobModel.HouseProperties.StringValue['CaseDir'] := GetCurrentDir;
DerobModel.HouseProperties.StringValue['StartDir'] := StartDir;
SetCurrentDir(StartDir);
UpdateGeometryPanel;
mainUpdateComboBox;
LoadClimateFiles;
UpdatePropertiesPanel;
UpdateEnergyPanel;
UpdateAbsorption;
UpdateClimatePanel;
UpdateClimate;
mainHide;
Geometry.IsSelected := True;
GeometryPanel.Visible := True;
TreeView1.Enabled := True;
TreeView1.HitTest := True;
DerobModel.HouseProperties.BoolValue['GlazeChange'] := False;
end;
end;
Can anyone help me out it's driving me nuts. I mean the only difference is the function LoadClimateFiles but that's called in one of the other functions instead now. The debugger says that the InitialDir is where I want it to be and that the FileName property is ''
Upvotes: 2
Views: 5623
Reputation: 31
How to work around that problem
If you know the correct path, then set a dummy file like
opendialog1.filename := 'C:\folder1*.doc';
That opens the right folder. In that case, initialdir is not needed!
Upvotes: 3
Reputation: 163357
This behavior is normal. See the documentation for the OpenFileName.lpstrInitialDir
field:
The initial directory. The algorithm for selecting the initial directory varies on different platforms.
Windows 7:
- If
lpstrInitialDir
has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.- Otherwise, if
lpstrFile
contains a path, that path is the initial directory.- Otherwise, if
lpstrInitialDir
is notNULL
, it specifies the initial directory.- If
lpstrInitialDir
isNULL
and the current directory contains any files of the specified filter types, the initial directory is the current directory.- Otherwise, the initial directory is the personal files directory of the current user.
- Otherwise, the initial directory is the Desktop folder.
Windows 2000/XP/Vista:
- If
lpstrFile
contains a path, that path is the initial directory.- Otherwise,
lpstrInitialDir
specifies the initial directory.- Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
- If
lpstrInitialDir
isNULL
and the current directory contains any files of the specified filter types, the initial directory is the current directory.- Otherwise, the initial directory is the personal files directory of the current user.
- Otherwise, the initial directory is the Desktop folder.
You've been passing the same value each time, so rule 1 applies. The OS ignores the directory your program asks for and uses the one the user prefers instead. You probably see different behavior in your older program because you've changed its behavior over time, and it no longer requests the same initial directory that it did the first time you ran the program.
You probably shouldn't worry about it.
Upvotes: 5