Embedded forms - accessing procedures on parent forms

Post your Gambas programming questions here.
Post Reply
bazzvn
Posts: 16
Joined: Wednesday 22nd February 2017 11:06am
Location: Vietnam

Embedded forms - accessing procedures on parent forms

Post by bazzvn »

In some of my programs I have been using embedded forms 3 levels deep - i.e. a parent form, a child form on the parent form, and a grandchild form on the child form. For the most part this works well, but I have run into problems trying to execute a procedure on a child form (2nd level) from a grandchild form (3rd level). I'm sure it's possible, but I don't know how to do it. I keep getting an error message (15; Null object). I attach a simple example project with code of what I am trying to do. I would be grateful if somebody could have a quick look at the code and point me in the right direction.

bazzvn
Attachments
Subforms-0.0.1.tar.gz
(12.15 KiB) Downloaded 10 times
User avatar
BruceSteers
Posts: 1059
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Embedded forms - accessing procedures on parent forms

Post by BruceSteers »

It's because of this...


Public hChild As FChild

Public Sub Form_Open()
  
 Dim hChild As New FChild(pnlChild)
  
End



Loose the Dim
By using Dim hChild you are making another new hChild variable local to Form_Open() not the public declaration so the
Public hChild As FChild never gets used so it remains to be Null , hence the error message..

try this...

Public hChild As FChild

Public Sub Form_Open()
  
 hChild = New FChild(pnlChild)
  
End



My guess would be you already know all this but that little "Dim" slipped your attention :)
If at first you don't succeed , try something different.
BruceS
bazzvn
Posts: 16
Joined: Wednesday 22nd February 2017 11:06am
Location: Vietnam

Re: Embedded forms - accessing procedures on parent forms

Post by bazzvn »

Thank you Bruce for such a simple solution. Sadly I did not know about this, which would have saved me lots of somewhat ugly workarounds in the past. I work mainly with sqlite databases, and the reason I like embedded subforms is that they enable me to encapsulate and localize code for viewing and manipulating specific tables and records in my databases, and I find that it helps a lot in tracking bugs.
bazzvn
User avatar
BruceSteers
Posts: 1059
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Embedded forms - accessing procedures on parent forms

Post by BruceSteers »

bazzvn wrote: Friday 27th January 2023 12:44pm Thank you Bruce for such a simple solution. Sadly I did not know about this, which would have saved me lots of somewhat ugly workarounds in the past. I work mainly with sqlite databases, and the reason I like embedded subforms is that they enable me to encapsulate and localize code for viewing and manipulating specific tables and records in my databases, and I find that it helps a lot in tracking bugs.
bazzvn
Much of the gambas ide is written with forms embeded in other forms.
As are many of my programs.

It sure has some huge benefits, cleaner code, easier to track code/bugs :)

And yes Dim is specifically for defining variables within a Sub or Function that are local only to that function.
The variables can have the same name as global variables.

You're welcome :)
If at first you don't succeed , try something different.
BruceS
Post Reply