user3464658
user3464658

Reputation: 229

Can't make OpenDialog window open in InitialDir

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

Answers (2)

Rhinolophus
Rhinolophus

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

Rob Kennedy
Rob Kennedy

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:

  1. 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.
  2. Otherwise, if lpstrFile contains a path, that path is the initial directory.
  3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
  4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
  5. Otherwise, the initial directory is the personal files directory of the current user.
  6. Otherwise, the initial directory is the Desktop folder.

Windows 2000/XP/Vista:

  1. If lpstrFile contains a path, that path is the initial directory.
  2. Otherwise, lpstrInitialDir specifies the initial directory.
  3. 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.
  4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
  5. Otherwise, the initial directory is the personal files directory of the current user.
  6. 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

Related Questions