FormInForm and Eventhandling

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
paco
Posts: 15
Joined: Friday 11th February 2022 5:42pm

FormInForm and Eventhandling

Post by paco »

Hi, I'd like to get userdefined events from an embedded form in its parent form like this:

Code: Select all

' Form1 (parent)
Public Sub Form_Open()
  Form2.Load(Me)
End

Public Sub Form2_AnyEvent(sData As String)
  Debug sData
End

' Form2 (embedded)
Event AnyEvent(Data As String)

Public Sub _new()
  Dim bResult As Boolean
  bResult = Raise AnyEvent("AnyData")
  If bResult Then Debug "canceled"
End
But that's not working. I know, the embedded Form keeps its own Observer, but shouldn't the parent receive those events? What's wrong in this code?

Thanks!
User avatar
BruceSteers
Posts: 1606
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: FormInForm and Eventhandling

Post by BruceSteers »

Its the ide sets up the observer for Form1 when it compiles/runs.

Because you add Form2 manually you'll need to set up your own observer.

Public Sub Form_Open()

  Form2.Load(Me)
  Dim hObs As Observer = New Observer(Form2) As "Form2"

End


I assume the above will work but I never really use Form.Load()

Or you can add Form2 to Form1 like this...
Public Sub Form_Open()

  Dim f2 As Form2 
  f2 = New Form2(Me) As "Form2"

End



The f2 can be local and use Last in the events to refer to it.

Or make f2 public (and a better name)

Private FForm2 As Form2 

Public Sub Form_Open()

  FForm2 = New Form2(Me) As "Form2"

End

If at first you don't succeed , try doing something differently.
BruceS
Post Reply