Common Dialog Management CU. Cancel action

Sl1m4erSl1m4er Member Posts: 42
Hi, guys.

Is there any appropriate way to get the result of the Cancel button being pressed in the opened dialog?

I've tried to add the following code in CU 412, before calling ShowOpen or ShowSave methods:
CommonDialogControl.CancelError := TRUE;

But the resulting message is so ugly. :shock:

Microsoft Dynamics NAV Classic
This message is for C/AL programmers:

The call to member ShowSave failed. CommonDialog returned the following message:
Cancel was selected.

Of course I could rely on the fact that in case the result returned by CommonDialogManagement.OpenFile is equal to DefaultFileName then the Cancel was pressed. This works great in case FileName is just the file name without the path.
FileName := EIEHeader."File Name";
WorkBookPath := ComDialogMgt.OpenFile('',FileName,4,Text027,1);
//this is supposed to be Cancel
IF WorkBookPath = FileName THEN
  EXIT;

But in my case FileName is predefined by the user during setup and it can contain the full path to the file. For example like this: C:\Users\Public\50162. And this path should be opened by default.

Thus when I open the dialog, FileName = C:\Users\Public\50162 and just press Save then the returned WorkBookPath = C:\Users\Public\50162 - the system just stops executing the code, because WorkBookPath = FileName.

In case I remove this code:
//this is supposed to be Cancel
IF WorkBookPath = FileName THEN
  EXIT;

The above scenario works fine but when I press Cancel in the opened dialog, the system proceeds with saving/opening the file.

I would appreciate any ideas on the issue described.

Answers

  • BeliasBelias Member Posts: 2,998
    Never mind, wrong suggestion
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • BeliasBelias Member Posts: 2,998
    in other words, you want to catch the "Cancel" event when the user press it.
    you can use the "IF codeunit.run then" trick...search mibuso for "if codeunit.run"...this should do the work for you
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • Sl1m4erSl1m4er Member Posts: 42
    Hello, Belias.
    in other words, you want to catch the "Cancel" event when the user press it.

    Exactly.
    you can use the "IF codeunit.run then" trick...search mibuso for "if codeunit.run"...this should do the work for you

    I'm not sure how this can help. Can you please clarify what do you mean? Do you suggest to modify Common Dialog Management CU?

    For now the only solution I've seen and that is used around in NAV is that FileName parameter passed to the ComDialogMgt.OpenFile or ComDialogMgt.OpenFileWithName is blank. Thus it is easy to handle the situation with Cancel button being pressed like this:
    WorkBookPath := ComDialogMgt.OpenFile('',FileName,4,Text027,1);
    IF WorkBookPath = '' THEN
      EXIT;
    
  • garakgarak Member Posts: 3,263
    if u use this:
    FileName := 'WhatIsTheInitValue?';
    WorkBookPath := ComDialogMgt.OpenFile('',FileName,4,Text027,1);
    IF WorkBookPath = '' THEN
      EXIT; 
    

    and the variable FileName isn't BLANK at the begin, then the WorkBookPath isn't empty. It has then the value of FileName. So, if WorkBookPath = FileName then the user press cancel.
    Do you make it right, it works too!
  • Sl1m4erSl1m4er Member Posts: 42
    garak wrote:
    if u use this:
    FileName := 'WhatIsTheInitValue?';
    WorkBookPath := ComDialogMgt.OpenFile('',FileName,4,Text027,1);
    IF WorkBookPath = '' THEN
      EXIT; 
    

    and the variable FileName isn't BLANK at the begin, then the WorkBookPath isn't empty. It has then the value of FileName.

    Hi, garak.

    Yes, and that's the problem in my case. FileName is predefined by the user in a setup form and it is never empty in my code. If I had FileName = '' all the time before OpenFile is run I woul never start this thread. ;)
  • BeliasBelias Member Posts: 2,998
    edited 2009-08-21
    1. create a global variable boolean in Cu 412: GlobalShowerror
    create a function in codeunit 412:
    ---function---
    globalshowerror := true
    
    add this code to cu412.openfile function:
    CommonDialogControl.CancelError := globalshowerror;
    

    create a new codeunit with a getparameter function in it: pass to this function all the necessary parameters you need for codeunit 412; in the code of this function, flush all these parameters to global variables.
    in the onrun of this codeunit:
    codeunit412.---function---
    codeunit412.openfile(pass all the global variables you just flushed)
    

    then in your source code, instead of calling the standard codeunit412, call:
    mynewcodeunit.getparameter(all pars for codeunit 412)
    if mynewcodeunit.run then begin
      //Common dialog succesful
    end else begin
      //Common dialog failed -manage the error as you want
    end;
    
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • garakgarak Member Posts: 3,263
    read the last senstens

    Also u can modify the CU412 to use the CancelError(BOOL) function of the "Microsoft Common Dialog Control".

    http://msdn.microsoft.com/en-us/library/aa226974(VS.60).aspx

    regards
    Do you make it right, it works too!
  • garakgarak Member Posts: 3,263
    @Belias: same idea. congrats
    Do you make it right, it works too!
  • Sl1m4erSl1m4er Member Posts: 42
    Belias wrote:
    1. create a global variable boolean in Cu 412: GlobalShowerror
    create a function in codeunit 412:
    ---function---
    globalshowerror := true
    
    add this code to cu412.openfile function:
    CommonDialogControl.CancelError := globalshowerror;
    

    create a new codeunit with a getparameter function in it: pass to this function all the necessary parameters you need for codeunit 412; in the code of this function, flush all these parameters to global variables.
    in the onrun of this codeunit:
    codeunit412.---function---
    codeunit412.openfile(pass all the global variables you just flushed)
    

    then in your source code, instead of calling the standard codeunit412, call:
    mynewcodeunit.getparameter(all pars for codeunit 412)
    if mynewcodeunit.run then begin
      //Common dialog succesful
    end else begin
      //Common dialog failed -manage the error as you want
    end;
    

    Thanks, Belias. I've got the idea and going to try it.
    garak wrote:
    read the last senstens

    Also u can modify the CU412 to use the CancelError(BOOL) function of the "Microsoft Common Dialog Control".

    http://msdn.microsoft.com/en-us/library/aa226974(VS.60).aspx

    regards

    Thanks, garak. If FileName = WorkBookPath - in my code means that user decided not to change the default path he had already set up, and wants to save it to FileName path. But in case I use IF FileName = WorkBookPath THEN EXIT statement, I will not be able to save it to the default path.

    Also thanks for the link.
  • garakgarak Member Posts: 3,263
    So, you're welcome.

    Regards
    Do you make it right, it works too!
  • BeliasBelias Member Posts: 2,998
    garak wrote:
    @Belias: same idea. congrats
    :thumbsup: thanks...BTW, the link show me "Content not found" :(
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • garakgarak Member Posts: 3,263
    edited 2009-08-21
    Belias wrote:
    garak wrote:
    @Belias: same idea. congrats
    :thumbsup: thaks...BTW, the link show me "Content not found" :(

    copy it, the forum cut the .asp, and paste it in the address field of your browser.

    Here the Link that should work:

    http://msdn.microsoft.com/en-us/library/aa226974(VS.60).aspx
    Do you make it right, it works too!
  • Sl1m4erSl1m4er Member Posts: 42
    Belias wrote:
    garak wrote:
    @Belias: same idea. congrats
    :thumbsup: thanks...BTW, the link show me "Content not found" :(

    Use the updated one ;): http://msdn.microsoft.com/en-us/library/aa226974(VS.60).aspx
  • BeliasBelias Member Posts: 2,998
    just noticed it, thanks
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • garakgarak Member Posts: 3,263
    And thats the link http://msdn.microsoft.com/en-us/library/aa259661(VS.60).aspx for the "CommonDialog Control" where all Methods and properties are described.

    Regards.

    @Sl1m4er: if you found it is a good solution for you and your problem is solved, please edit the first post and set there the attribute (it's on top).
    Do you make it right, it works too!
  • Sl1m4erSl1m4er Member Posts: 42
    Belias, garak

    Your suggestion to use wrapper around 412 CU worked for me great! \:D/

    Thank you for your help once again. I really appreciate it. ;)
Sign In or Register to comment.