Recent

Author Topic: [solved] open dialog - default directory  (Read 2274 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1009
[solved] open dialog - default directory
« on: April 24, 2024, 08:02:40 pm »
I work with Win 10

There is an opendialog. And I want it to start in always the same default diretory.
Unfortunately it starts in some directory either where I had been for the last time or where I opened the thing, I am not sure.

I tried:

Code: Pascal  [Select][+][-]
  1. OpenDialog1.InitialDir:='myDefault_Path';

but this does not work.
What to change?

This sequence is from my code:

 
Code: Pascal  [Select][+][-]
  1. OpenDialog_RichMemoNotizen.InitialDir:=Pfad + '\Texte'; // dort werden rtf Dateien primär gesucht
  2.  
  3.   if OpenDialog_RichMemoNotizen.Execute then begin
  4.     datei := OpenDialog_RichMemoNotizen.Filename;
  5.  
  6.     If ExtractFileExt(datei) = '.txt'
  7.       then begin
  8.         RichMemo_wichtig.Lines.LoadFromFile(datei);
  9.         ShowMessage('Die Textdatei wird zwar geladen, doch ein Speichern auch als rtf führt zum Einfügen von Formatierungszeichen. D.h. der Text sollte hier nicht bearbeitet werden.');
  10.       end
  11.       else LoadRTFFile(RichMemo_wichtig, datei);
  12.  
  13.     IstVeraendert(self);
  14.     RichMemoName:= datei; // OpenDialog_RichMemoNotizen.Filename; // diese Var hält den Pfad der Datei im Arbeitsspeicher
  15.     Caption := RichMemoName;
  16.     end;
  17.                        
« Last Edit: April 27, 2024, 03:52:11 pm by Nicole »

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: open dialog - default directory
« Reply #1 on: April 25, 2024, 12:16:58 am »
as far as I know, it has never worked in the way it's documented.

 you need to Set your FILENAME with the folder path attached to it.

FileName := 'THe_Directory\'+datei

The InitDir will report where the first folder was pointed to when you exit the dialog.


The only true wisdom is knowing you know nothing

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: open dialog - default directory
« Reply #2 on: April 25, 2024, 12:34:53 am »
Here it works with absolute path all the time good.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   OpenDialog1.InitialDir := 'c:\downloads\';
  4.   if OpenDialog1.Execute then
  5.     ;
  6. end;
I can open, switch to different folders or volumes, selecting something, pressing okay, re-open dialog and I am again in my set folder and not the last.
I did not altered any options, just placed on form.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: open dialog - default directory
« Reply #3 on: April 25, 2024, 02:24:45 am »
I believe the problem that is happening is that the "InitialDir" gets changed to the path when the dialog closes and thus is no longer valid.
 
 So if you were to reuse this value later on in code, it will not be as it was.

 You can see this in the dialog code if it finds a path in the FILENAME it resets the "InitialDir" to that path.

The only true wisdom is knowing you know nothing

jcmontherock

  • Sr. Member
  • ****
  • Posts: 271
Re: open dialog - default directory
« Reply #4 on: April 25, 2024, 11:02:37 am »
Maybe:
  openDialog1.InitialDir := GetCurrentDir;
and after, in save file:
  SetCurrentDir(ExtractFileDir(sFileName));
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: open dialog - default directory
« Reply #5 on: April 25, 2024, 01:59:36 pm »
Thank you for all answers.

Unfortunately nothing helps. The directory is not the one, I want to.
I tried to set the path indirectly by full file-name, as well as I tried to set current path.

May Windows "overrule" this?
I use Win 10 on the host and Win 7 on the VM, where I develop my software.

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: open dialog - default directory
« Reply #6 on: April 25, 2024, 04:10:02 pm »
There is an opendialog. And I want it to start in always the same default diretory.
Unfortunately it starts in some directory either where I had been for the last time or where I opened the thing, I am not sure.
Are you really sure that this is a good idea? I absolutely hate software in which the file dialog always opens in the same directory, but my files are in a different directory so that I am forced to navigate to "my" directory every time the file dialog is needed.

If, for some reason, the files definitely are in that well-defined directory only, then probably the TOpenDialog is not what you need at all? Maybe a TShellListView (without ShellTreeView) or a TFileListBox, hard-wired to that directory, is sufficient?
« Last Edit: April 25, 2024, 04:44:37 pm by wp »

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: open dialog - default directory
« Reply #7 on: April 25, 2024, 04:19:51 pm »
Thank you for all answers.

Unfortunately nothing helps. The directory is not the one, I want to.
I tried to set the path indirectly by full file-name, as well as I tried to set current path.

May Windows "overrule" this?
I use Win 10 on the host and Win 7 on the VM, where I develop my software.

You did see, that KodeZwerg's sample ends with a backslash? (Compared to yours)

That said, i looked at the documentation and found this
https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/topendialog.options.html
https://lazarus-ccr.sourceforge.io/docs/lcl/dialogs/topenoption.html

especially this one is interesting
Quote
TOpenOption = (
    ofReadOnly,
    ofOverwritePrompt, // if selected file exists shows a message, that file
                       // will be overwritten
    ofHideReadOnly,    // hide read only file
    ofNoChangeDir,     // do not change current directory
    ofShowHelp,        // show a help button
    ofNoValidate,
    ofAllowMultiSelect,// allow multiselection
.
.
.
.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: open dialog - default directory
« Reply #8 on: April 27, 2024, 03:51:54 pm »
Thank you so much, now it works.

I have to set both CurrentDir and initialDir

   SetCurrentDir(Text_Pfad);
   OpenDialog_RichMemoNotizen.InitialDir:=Text_Pfad;

The slash at the end was an important hint.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: open dialog - default directory
« Reply #9 on: April 27, 2024, 04:13:10 pm »
You did see, that KodeZwerg's sample ends with a backslash? (Compared to yours)
Unfortunately nothing helps.
Since she wrote that I knew already that she has not tried my code :D
   SetCurrentDir(Text_Pfad);
I hope you do know the consequences of such action.
Or in other words, it might work as you want in your current project but it can badly fail for others where you load/save file(s) and assume that the current folder is your project folder and do "\text\data.txt" in hope that FPC put the correct path infront of it :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: [solved] open dialog - default directory
« Reply #10 on: April 27, 2024, 05:53:51 pm »
There was an additional problem: The file was on the host and the code was on the VM.
There was a mess of slashes.

I am afraid, I do not understand about the consequences still:
The path is for my private notes, which are very slim and on a long path.
There I read e.g. static pharmacy list: eye drops for computer work.
Just lists you would forget about if times pass by.

My usual work I do in the Windows documents folder. A heavy load for backup and organized by the explorer.
My slim notes are slim organized.

Now about the consequences: I am afraid, I do not understand them at all.
When I work with my notes, the path of the file is written in the Form's header and there I can click "save" and "save under" and there I hope to see, what I am doing.
Will this work or not?

Will I still be able to save a file in case in a different location, the moment I navigate the Windows explorer to it?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [solved] open dialog - default directory
« Reply #11 on: April 27, 2024, 08:49:40 pm »
@Nicole
Let me explain it differently so you can answer it yourself.
Imagine you have a folder called "text" for text files within your application.exe folder, imagine you would load or save a file like "Memo1.LoadFromFile('.\text\file.txt');"
As long you do not call SetCurrentDir() everything is okay working, afterwards not anymore since the working path is changed by you.
So I would suggest to call such like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   OldDir: AnsiString;
  3. begin
  4.   // store current working path
  5.   OldDir := GetCurrentDir;
  6.   // set new path and only on success do more
  7.   if SetCurrentDir(thepath) then
  8.     begin
  9.       // do your actions and this as last call:
  10.       SetCurrentDir(OldDir);
  11.     end;
  12. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: [solved] open dialog - default directory
« Reply #12 on: April 30, 2024, 04:28:43 pm »
You got me, that I have no idea, what "currentDir" really means.
I just copied the line and was happy it worked.

Still not sure about the consequences. Is it crucial, which is the working directory? (I risk, that you say, this is a very stupid question).
The files I use are set in the create event.

My organization is like this (as your name is KodeZwerg, I suspect you understand German) in OnCreate

Code: Pascal  [Select][+][-]
  1.   // findet den Ordner "Eigene Dateien" in Win 7 und Win 10
  2.   Pfad:=GetWindowsSpecialDir(CSIDL_PERSONAL);  // benöigt windirs  
  3.   Pfad:=Pfad + '\TermineAb_2022\';
  4.  
  5.   Pfad_Texte:=Pfad + 'Texte\'; // 7/ 22: zfür Textdateien
  6.   Pfad_Sound:=Pfad + 'hilfsdateien\Sound\'; // 7/ 22:  Signalton  


So my problem is (was?) this:
When I opened a file from e.g. downloads, the way to my text-directories means quite an annoying high amount of clicks to get back there.

The open method is this

Code: Pascal  [Select][+][-]
  1. // öffnet eine übergebene Datei für RichMemo default in rtf-Format oder startet den open-Dialog
  2. procedure TForm_Notizen.ToolButton_oeffneClick(Datei: string);
  3. Var ch: PChar;
  4.     s: string;
  5.     Text_Pfad: string;
  6. begin
  7.    s:=  'Änderungen speichern in ' + RichMemoName+ '?'; // formt s in PChar
  8.    ch:=PChar(s);
  9.  
  10.   if not Saved_dasRichMemo then   // ungespeicherte Änderungen sichern
  11.        case Application.MessageBox(ch, 'Achtung', MB_YESNOCANCEL)
  12.         of IDYES    : ToolButton_speichernClick(self); // Speichern; diese Medhode auflassen, sie funktioniert nicht
  13.            IDCANCEL : exit;
  14.         end;
  15.  
  16.    Text_Pfad:=Pfad + 'Texte\'; // dort werden rtf Dateien primär gesucht
  17.    SetCurrentDir(Text_Pfad);
  18.    OpenDialog_RichMemoNotizen.InitialDir:=Text_Pfad;
  19.  
  20.   if OpenDialog_RichMemoNotizen.Execute then begin
  21.      datei := Text_Pfad + OpenDialog_RichMemoNotizen.Filename;
  22.  
  23.     If ExtractFileExt(datei) = '.txt'
  24.       then begin
  25.         RichMemo_wichtig.Lines.LoadFromFile(datei);
  26.         ShowMessage('Die Textdatei wird zwar geladen, doch ein Speichern auch als rtf führt zum Einfügen von Formatierungszeichen. D.h. der Text sollte hier nicht bearbeitet werden.');
  27.       end
  28.       else LoadRTFFile(RichMemo_wichtig, datei);
  29.  
  30.     IstVeraendert(self);
  31.     RichMemoName:= datei; // OpenDialog_RichMemoNotizen.Filename; // diese Var hält den Pfad der Datei im Arbeitsspeicher
  32.     Caption := RichMemoName;
  33.     end;
  34. end;


 

TinyPortal © 2005-2018