← servoy magazine

[Tip] Preventing a dialog from closing

  • Tips

by Marcel Trapman
www.it2be.com

Sometimes you don't want the user to be able to close a dialog other than via the buttons you place on the form (dialog). You can do this by attaching methods to the onHide event, onShowForm event, your OK button and your Cancel button. Here are the methods:

onShow

trigger = null;


I define a form variable 'trigger' and set it to null to begin with.

onHide

if (!trigger) {
   return false;
}

Here I tell the form not to hide itself if the variable 'trigger' is null, "" or 0...

selectedCancel

trigger = "cancel";
application.closeFormDialog();


The cancel button is triggered and the variable 'trigger' is set to the value 'cancel'. Because 'trigger' is not emply anymore the evaluation in the onHide event will not return false.

selectedOK

your own code;
trigger = "ok";
application.closeFormDialog();


In fact this is the same as the method for the cancel button except that you will put in the code that needs to be executed here before you close the dialog.

One important thing before testing this: ENABLE THE DEBUGGER... If you don't and your code is wrong there is nothing left but to cancel Servoy Developer with a force quit.

Cheers,

Marcel

Comments (2)

Tara Ingram
Thank you for the tip on this very useful feature. If I'm reading this right, it appears to be related to using the showFormInDialog. I'm wondering also about preventing the closing of the Plugins.dialogs (showInfoDialog, showErrorDialog, etc).
Marcel Trapman
Hmm, I don't see the relevance of that. When you open a form with showFormInDialog you could (not necessarily so) be asking something from the user he NEEDS to give an answer for. With a dialog simply using the close box cancels the operation and is the same as a 'cancel' button. BTW as far as I know this behavior is platform dependent and defined.