There is a 4D command that allows developers to execute a method in a subform from the parent form, but not to do the opposite. This Technical Tip demonstrates just that, how to execute a method in a parent form called from a subform.
An invisible button can be added to the parent form, assigning a shortcut to the button. This provides a way to trigger an even on the parent form from the subform. In the subform, the command POST KEY can be used to trigger the On Clicked event of the invisible button on the parent form. Simply have the desired method executed when the On Clicked form event is triggered.
Below is an example of a method being executed on the parent form from the subform.
Here is an image of a form that has a button, an invisible button and a single selection output subform:
In this example, the visible button should only be displayed when the selected record in the subform has the enableButton field checked. To do this, the invisible button is assigned a shortcut (Ctrl+9 is used in this example). The object method for the invisible button has the following code:
Case of : (Form event=On Load) subformCondition:=True : (Form event=On Clicked) If (subformCondition) OBJECT SET ENABLED(*;"Button";True) Else OBJECT SET ENABLED(*;"Button";False) End if End case |
The variable subformCondition is set in the subform method when the On Selection Change form event is triggered:
Case of : (Form event=On Selection Change) subformCondition:=[Table_2]enableButton POST KEY(Character code("9");Command key mask) End case |
When the selection in the subform is changed, the subformCondition variable is set to the value of the enableButton field. Then the command POST KEY is called to trigger the On Clicked event of the invisible button which will enable or disable the visible button located on the parent form.