Calling a function from within a Template form

I have a main form (let’s call it Form_1) and at one point I clear its content_panel and replace it with another form (let’s call it Form_2).

On Form_2, I have some buttons inside a grid panel.

The call back function for the buttons is inside the “grid template form” that gets created.

When I click the button I need to call a function that is on Form_2. However, when I use:
get_open_form().my_function(), it refers back to Form_1 and tells me that “my_function” does not exist.

I would have thought that get_open_form() would refer to the form I opened up last, which was Form_2.

What am I missing here?

Al

What you’ve done is open Form1 as the main open form with the function open_form().
You’ve then added Form2 as a component to Form1 (so Form1 is the parent of Form2).

You could do :

get_open_form().Form2.my_function()

Thanks David,

This worked (haha):
self.parent.parent.parent.parent.parent.my_function()

The odd thing is that I have two grid panels on Form_2, they both have buttons inside a repeating panel, and for some reason get_open_form().my_function() works when I click the buttons in the first grid panel, but not the second. In other words, inside the grid template (from the first grid panel), get_open_form().my_function() allows me to access Form_2.

As both grid panels are on the same form I can’t see the difference.

Thanks for the suggestion. It is working for now, and who doesn’t like a bunch of “parents” :wink:

:slight_smile: that’s a little fragile, despite it actually working.
You should maybe keep a reference to the form in Form1. For example :

self.my_new_form = Form2()
self.content_panel.add_component(self.my_new_form)

then you could do this from anywhere :

get_open_form().my_new_form.my_function()
1 Like

Well, that just works perfectly. I never though of “sticking” a reference to the new form on the original form. Very clever man.

Thanks very much.
Al

1 Like