Recent

Author Topic: Actions and Reslts  (Read 855 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Actions and Reslts
« on: June 05, 2023, 08:29:12 am »
Hello,

i have an Action which handles the Saving for me.
This Code is in my Saving algorithm:

Code: Pascal  [Select][+][-]
  1.          if FileExists(FilePath) then begin
  2.             if CustomMessage('', Get_TxInLng(4263), cmQuestion) <> mrYes then begin
  3.                Cursor:= crDefault;
  4.                Exit;
  5.             end;
  6.          end;
  7.  

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][+][-]
  1. actFileSaveAs.Execute;
  2.  

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 !
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Actions and Reslts
« Reply #1 on: June 05, 2023, 08:58:20 am »
?!?!?! --> https://wiki.freepascal.org/Howto_Use_TSaveDialog
Quote
The Execute method displays the file save dialog. It returns true when user has selected a file, false when user has aborted.
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

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Actions and Reslts
« Reply #2 on: June 05, 2023, 09:22:07 am »
hey I have described the problem incorrectly.
This is my save dialog:

Code: Pascal  [Select][+][-]
  1. procedure TStartUp.actFileSaveAsAccept(Sender: TObject);
  2. var
  3.    FilePath: string;
  4. begin
  5.    Cursor:= crHourGlass;
  6.    if actFileSaveAs.Dialog.Execute then begin
  7.       FilePath:= actFileSaveAs.Dialog.FileName;
  8.       if not (acPath= FilePath) then begin
  9.          if FileExists(FilePath) then begin
  10.             if CustomMessage('', Get_TxInLng(4263), cmQuestion) <> mrYes then begin
  11.                Cursor:= crDefault;
  12.                Exit;
  13.             end;
  14.          end;
  15.          //Save
  16.          try
  17.            //.....
  18.          except
  19.             on e:Exception do begin
  20.                CustomMessage('', 'failed to Save File ! '+sLineBreak+'Exception raised: '+e.Message+'', cmError);
  21.             end;
  22.          end;
  23.       end;
  24.    end;
  25.    Cursor:= crDefault;
  26. end;
  27.  

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][+][-]
  1. actFileSaveAs.Execute;
  2.  

can i somehow get back any result from the Execute method ?
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Actions and Reslts
« Reply #3 on: June 05, 2023, 09:29:42 am »
can 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

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Actions and Reslts
« Reply #4 on: June 05, 2023, 09:31:51 am »
can 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
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Actions and Reslts
« Reply #5 on: June 05, 2023, 09:35:52 am »
i 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)
« Last Edit: June 05, 2023, 09:44:03 am by TRon »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Actions and Reslts
« Reply #6 on: June 05, 2023, 09:48:26 am »
i attached a Screenshot, i do not have an OnExecute Handler.
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Actions and Reslts
« Reply #7 on: June 05, 2023, 09:50:05 am »
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

  • Hero Member
  • *****
  • Posts: 2319
Re: Actions and Reslts
« Reply #8 on: June 05, 2023, 09:51:06 am »
You do see the "OnCancel"-Event???
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

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Actions and Reslts
« Reply #9 on: June 05, 2023, 09:53:21 am »
i 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

  • Hero Member
  • *****
  • Posts: 2435
Re: Actions and Reslts
« Reply #10 on: June 05, 2023, 09:58:03 am »
This 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

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Actions and Reslts
« Reply #11 on: June 05, 2023, 09:59:43 am »
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.

i will try it this way thanks
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Actions and Reslts
« Reply #12 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/tcommondialogaction.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

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Actions and Reslts
« Reply #13 on: June 05, 2023, 10:28:30 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/tcommondialogaction.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
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Actions and Reslts
« Reply #14 on: June 05, 2023, 10:40:17 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.
« Last Edit: June 05, 2023, 10:45:44 am by wp »

 

TinyPortal © 2005-2018