Actions and Reslts lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 06:29 Hello, i have an Action which handles the Saving for me. This Code is in my Saving algorithm: 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          if FileExists(FilePath) then begin            if CustomMessage('', Get_TxInLng(4263), cmQuestion) <> mrYes then begin               Cursor:= crDefault;               Exit;            end;         end;  which is checking if the File Exists and then asks if it should be overriden. Now i have an OpenProject Action too. But that action checks if the current project is saved, if that is not the Case then i call 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 actFileSaveAs.Execute;  can i somehow check the result of the save dialog ? i need to check if the Save Dialog was canceled. im fairly new to Actions / ActionList, so any hints would be appreciated. Thanks ! ---------------------------------------------------------------------- Zvoni · 2023-06-05 06:58 ?!?!?! --> https://wiki.freepascal.org/Howto_Use_TSaveDialog QuoteThe Execute method displays the file save dialog. It returns true when user has selected a file, false when user has aborted. ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 07:22 hey I have described the problem incorrectly. This is my save dialog: 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 TStartUp.actFileSaveAsAccept(Sender: TObject);var   FilePath: string; begin   Cursor:= crHourGlass;   if actFileSaveAs.Dialog.Execute then begin      FilePath:= actFileSaveAs.Dialog.FileName;      if not (acPath= FilePath) then begin         if FileExists(FilePath) then begin            if CustomMessage('', Get_TxInLng(4263), cmQuestion) <> mrYes then begin               Cursor:= crDefault;               Exit;            end;         end;         //Save         try         //.....         except            on e:Exception do begin             CustomMessage('', 'failed to Save File ! '+sLineBreak+'Exception raised: '+e.Message+'', cmError);            end;         end;      end;   end;   Cursor:= crDefault;end;  within the Code is this Line : if CustomMessage('', Get_TxInLng(4263), cmQuestion) <> mrYes then begin which will check if the File exists and if it should be overriden. if the user does abort this Dialog then i would like to give back a result if i do 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 actFileSaveAs.Execute;  can i somehow get back any result from the Execute method ? ---------------------------------------------------------------------- TRon · 2023-06-05 07:29 Quote from: Weitentaaal on June 05, 2023, 09:22:07 amcan i somehow get back any result from the Execute method ? I am not familiar with TActions but the documentation does state that the execute method returns a boolean value. So depending on what you return in your execute method ? ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 07:31 Quote from: TRon on June 05, 2023, 09:29:42 amQuote from: Weitentaaal on June 05, 2023, 09:22:07 amcan i somehow get back any result from the Execute method ? I am not familiar with TActions but the documentation does state that the execute method returns a boolean value. So depending on what you return in your execute method ? i have seen that but i don't know how to set the result value, if its even possible to set it ---------------------------------------------------------------------- TRon · 2023-06-05 07:35 Quote from: Weitentaaal on June 05, 2023, 09:31:51 ami have seen that but i don't know how to set the result value, if its even possible to set it From the 1 minute reading the documentation I would have to guess that you assign an onexecute event method to your action that actually implements saving the file. That method would then return a true/false value. ? Do note the quick read and my inexperience with TActions. edit: fwiw I though the routines you showed already represented the execute implementation. In any case also note that TAction is actually a TComponent and as such also has a tag property that you could abuse (though I would not recommend abusing it for these kind of things in particular as to me that seems to contradict the use for/of using actions) ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 07:48 i attached a Screenshot, i do not have an OnExecute Handler. ---------------------------------------------------------------------- wp · 2023-06-05 07:50 This way of doing it seems to be too complicated to me. Why don't you simply add the option odOverwritePrompt to the SaveAsAction.Dialog.Options? Or if you insist on your own error message you could implement a handler for the OnCanClose event of the SaveAsAction.Dialog in which you return CanClose as false if the file already exists and the user does not want to overwrite it. ---------------------------------------------------------------------- Zvoni · 2023-06-05 07:51 You do see the "OnCancel"-Event??? ---------------------------------------------------------------------- TRon · 2023-06-05 07:53 Quote from: Weitentaaal on June 05, 2023, 09:48:26 ami attached a Screenshot, i do not have an OnExecute Handler. It is an event of the action, not the dialog. You would have to look at your action list (and at the action that you linked to your dialog). ---------------------------------------------------------------------- TRon · 2023-06-05 07:58 Quote from: wp on June 05, 2023, 09:50:05 amThis way of doing it seems to be too complicated to me. For sure but if you really want to separate actual code from your visual components then that is how things like actions are suppose to work. ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 07:59 Quote from: wp on June 05, 2023, 09:50:05 amThis way of doing it seems to be too complicated to me. Why don't you simply add the option odOverwritePrompt to the SaveAsAction.Dialog.Options? Or if you insist on your own error message you could implement a handler for the OnCanClose event of the SaveAsAction.Dialog in which you return CanClose as false if the file already exists and the user does not want to overwrite it. i will try it this way thanks ---------------------------------------------------------------------- TRon · 2023-06-05 08:11 @weitentaaal: Somewhat longer reading the documentation states that the default savefileas action is a commondialog action that has a property named executeresult, https://lazarus-ccr.sourceforge.io/docs/lcl/stdactns/tc ommondialogaction.executeresult.html That is probably why the execute event is 'hidden'. I was looking at a 'normal' action when I made my comments. Apologies for the misinformation. ---------------------------------------------------------------------- Weitentaaal · 2023-06-05 08:28 Quote from: TRon on June 05, 2023, 10:11:47 am@weitentaaal: Somewhat longer reading the documentation states that the default savefileas action is a commondialog action that has a property named executeresult, https://lazarus-ccr.sourceforge.io/docs/lcl/stdactns/tc ommondialogaction.executeresult.html That is probably why the execute event is 'hidden'. I was looking at a 'normal' action when I made my comments. Apologies for the misinformation. sorry i should have mentioned that ! the ExecuteResult will only tell me about the dialog, everything else that happens in SaveAsAccept Handler will be ignored. So i still dont knpow if i have had an error in my Saving algorithm. But i will make additional functions to get it to work. it wont look like clean code but it does the job ---------------------------------------------------------------------- wp · 2023-06-05 08:40 Quote from: wp on June 05, 2023, 09:50:05 am[...] implement a handler for the OnCanClose event of the SaveAsAction.Dialog in which you return CanClose as false if the file already exists and the user does not want to overwrite it. There's something wrong with this, at least on Windows: In the attached project there is a handler for SaveAsAction.OnAccept (which just displays a "saved" message, but does nothing else, and a handler for SaveAsAction.Dialog.OnCanClose which checkes whether the file already exists and asks the user whether he wants to overwrite the original file. When I run this and select an existing file in the filedialog, deny the prompt to overwrite the file, and then close the FileDialog with "Cancel" the OnAccept handler is called nevertheless (with an empty filename, at least)... P.S. The empty filename was caused by myself resetting in the OnCanClose handler. So, the situation is quite risky since this way of doing it saves the file even if overwriting is denied and the file dialog has been cancelled! Attaching a modified version of the test program. ---------------------------------------------------------------------- TRon · 2023-06-05 08:52 @wp: if I check your app on Linux with 2.2.4 then I get an operation was cancelled messagebox when selecting cancel or no and after canceling the savedialog. I take it that is the intended behaviour ? edit: same results as above with trunk Lazarus. ---------------------------------------------------------------------- wp · 2023-06-05 09:13 Quote from: TRon on June 05, 2023, 10:52:22 am@wp: if I check your app on Linux with 2.2.4 then I get an operation was cancelled messagebox when selecting cancel or no and after canceling the savedialog. I take it that is the intended behaviour ? Yes. That's fine because it tells me that the bug is not in the general TFileDialog/TCommonDialog code, but in the win32 widgetset code. And in fact there is the win32-specific function TFileDialogEvents.OnFileOK which does not reset the FDialog.UserChoice in case of CanClose is false. With the following modification, the overall behaviour is correct, on Windows: 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 function TFileDialogEvents.OnFileOk(pfd: IFileDialog): HResult; stdcall;  // removed the {$ifdef...} instructionsvar  CanClose: Boolean;begin  Result := TWin32WSOpenDialog.ProcessVistaDialogResult(pfd, FDialog);  if Succeeded(Result) then  begin    FDialog.UserChoice := mrOK; //DoCanClose needs this    CanClose := True;    FDialog.DoCanClose(CanClose);    if CanClose then    begin      Result := S_OK;    end    else    begin      FDialog.UserChoice := mrNone;   // <---- ADDED      Result := S_FALSE;    end;  end;end; [EDIT] Filed a bug report for documentation (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40297), and committed above fix to Laz/main.