When a form is replaced by another form, either by the
get_open_form().content_panel.clear(), by the
open_form() or by whatever other technique is available in Anvil to close a form, the
query_close() event should be fired on the form that is disappearing.
This allows to wrap things up and, for example, ask the user whether to save the content of the form, and allow the user to cancel the form closing.
In the good old VB6 the
FormUnload event had the
Cancel argument that could be set to
True to prevent the form from closing.
I don’t know what the best way would be to achieve the same result in Anvil.
Perhaps the event should raise an exception, which would prevent the
get_open_form().content_panel.clear() or the
open_form() from doing their job.
Or perhaps the event should just return
False and the event code would look like this:
def form_query_close(self, **event_args): button = alert('Do you want to save your stuff?', buttons=[ ("Yes", "Yes", "danger"), ("No", "No", "info"), ("Cancel", "Cancel", "default") ]) if button == 'Cancel': return True if button == 'Yes': self.save()
And the form managing code would look like this:
if get_open_form().content_panel.clear(): return get_open_form().content_panel.add_component(NewForm())
Here is how I simulate the
query_close event. It is not difficult, but an official event would be more elegant.
def show_form(self, form, *args, **kwargs): try: current_form = get_open_form().content_panel.get_components() if current_form.query_close(): return except: pass get_open_form().content_panel.clear() get_open_form().content_panel.add_component(form(*args, **kwargs))