[solved] open dialog - default directory lazarus / 21-General ---------------------------------------------------------------------- Nicole · 2024-04-24 18:02 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OpenDialog1.InitialDir:='myDefault_Path'; but this does not work. What to change? This sequence is from my code:  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OpenDialog_RichMemoNotizen.InitialDir:=Pfad + '\Texte'; // dort werden rtf Dateien primär gesucht   if OpenDialog_RichMemoNotizen.Execute then begin    datei := OpenDialog_RichMemoNotizen.Filename;     If ExtractFileExt(datei) = '.txt'      then begin        RichMemo_wichtig.Lines.LoadFromFile(datei);        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.');      end      else LoadRTFFile(RichMemo_wichtig, datei);     IstVeraendert(self);    RichMemoName:= datei; // OpenDialog_RichMemoNotizen.Filename; // diese Var hält den Pfad der Datei im Arbeitsspeicher    Caption := RichMemoName;    end; ---------------------------------------------------------------------- jamie · 2024-04-24 22:16 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. ---------------------------------------------------------------------- KodeZwerg · 2024-04-24 22:34 Here it works with absolute path all the time good. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  OpenDialog1.InitialDir := 'c:\downloads\';  if OpenDialog1.Execute then    ;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. ---------------------------------------------------------------------- jamie · 2024-04-25 00:24 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. ---------------------------------------------------------------------- jcmontherock · 2024-04-25 09:02 Maybe:   openDialog1.InitialDir := GetCurrentDir; and after, in save file:   SetCurrentDir(ExtractFileDir(sFileName)); ---------------------------------------------------------------------- Nicole · 2024-04-25 11:59 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 · 2024-04-25 14:10 Quote from: Nicole on April 24, 2024, 08:02:40 pmThere 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? ---------------------------------------------------------------------- Zvoni · 2024-04-25 14:19 Quote from: Nicole on April 25, 2024, 01:59:36 pmThank 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 QuoteTOpenOption = (     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 . . . . ---------------------------------------------------------------------- Nicole · 2024-04-27 13:51 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 · 2024-04-27 14:13 Quote from: Zvoni on April 25, 2024, 04:19:51 pmYou did see, that KodeZwerg's sample ends with a backslash? (Compared to yours)Quote from: Nicole on April 25, 2024, 01:59:36 pmUnfortunately nothing helps.Since she wrote that I knew already that she has not tried my code Quote from: Nicole on April 27, 2024, 03:51:54 pm   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 ---------------------------------------------------------------------- Nicole · 2024-04-27 15:53 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 · 2024-04-27 18:49 @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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  OldDir: AnsiString;begin  // store current working path  OldDir := GetCurrentDir;  // set new path and only on success do more  if SetCurrentDir(thepath) then    begin      // do your actions and this as last call:      SetCurrentDir(OldDir);    end;end; ---------------------------------------------------------------------- Nicole · 2024-04-30 14:28 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // findet den Ordner "Eigene Dateien" in Win 7 und Win 10  Pfad:=GetWindowsSpecialDir(CSIDL_PERSONAL);  // benöigt windirs    Pfad:=Pfad + '\TermineAb_2022\';   Pfad_Texte:=Pfad + 'Texte\'; // 7/ 22: zfür Textdateien  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // öffnet eine übergebene Datei für RichMemo default in rtf-Format oder startet den open-Dialogprocedure TForm_Notizen.ToolButton_oeffneClick(Datei: string);Var ch: PChar;    s: string;    Text_Pfad: string;begin   s:=  'Änderungen speichern in ' + RichMemoName+ '?'; // formt s in PChar   ch:=PChar(s);   if not Saved_dasRichMemo then   // ungespeicherte Änderungen sichern       case Application.MessageBox(ch, 'Achtung', MB_YESNOCANCEL)        of IDYES    : ToolButton_speichernClick(self); // Speichern; diese Medhode auflassen, sie funktioniert nicht         IDCANCEL : exit;        end;    Text_Pfad:=Pfad + 'Texte\'; // dort werden rtf Dateien primär gesucht   SetCurrentDir(Text_Pfad);   OpenDialog_RichMemoNotizen.InitialDir:=Text_Pfad;   if OpenDialog_RichMemoNotizen.Execute then begin     datei := Text_Pfad + OpenDialog_RichMemoNotizen.Filename;     If ExtractFileExt(datei) = '.txt'      then begin        RichMemo_wichtig.Lines.LoadFromFile(datei);        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.');      end      else LoadRTFFile(RichMemo_wichtig, datei);     IstVeraendert(self);    RichMemoName:= datei; // OpenDialog_RichMemoNotizen.Filename; // diese Var hält den Pfad der Datei im Arbeitsspeicher    Caption := RichMemoName;    end;end;