Raising Event from Child Class to Parent Class

Shahabg

Member
Licensed User
Longtime User
Hi
I am new to b4a, I have a Child class which has a button, I want to pass the button click of child class to the parent class not the Main, like (raise event in vb).
Thanks
 

Shahabg

Member
Licensed User
Longtime User
Hi Erel,
Thanks for response,but I have the same problem

here is the code:
Main:
B4X:
Sub Globals
   Dim objParent As Parent
End Sub

Sub Activity_Create(FirstTime As Boolean)
   objParent.Initialize(Activity.Width , Activity.Height)
   Activity.AddView(objParent.MainPanel ,0,0,Activity.Width,Activity.Height)
End Sub
Parent: Class
B4X:
'Class module
Sub Class_Globals
  Dim pnlMain As Panel
   Dim ch1 As Child
   Dim ch2 As Child
End Sub

Public Sub Initialize(ptop As  Float, pheight As Float)
   pnlMain.Initialize("")
   pnlMain.Color=Colors.Red
   ch1.Initialize(50,50,"Bad", "BAD")
   ch2.Initialize(75,75, "Good", "Good")
   pnlMain.AddView(ch1.MainPanel, 0,0 , 100,100)
   pnlMain.AddView(ch2.MainPanel, 65, 65, 200,200)
End Sub
Public Sub MainPanel As Panel
    Return pnlMain
End Sub
Public Sub ChildManager(ch As Child, ptag As String)
   Msgbox ("child click" , "event")
End Sub
The Child class:
B4X:
Sub Class_Globals
   Dim pnlchild As Panel
   Dim btnChild As Button
End Sub
Public Sub Initialize(pwidth As Float, pheight As Float, pTag As String, txt As String)
   pnlchild.Initialize("")
   btnChild.Initialize("Child")
   btnChild.Tag=pTag
   btnChild.Text=txt
   pnlchild.AddView(btnChild, 20, 20, pwidth,pheight)
End Sub
Public Sub MainPanel() As Panel
Return pnlchild
End Sub
Sub Child_Click
Dim b As Button
   b = Sender
   CallSub2(Me , "ChildManager", b.tag)
End Sub
 
Last edited:
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
The Child class:
B4X:
Sub Class_Globals
   Dim pnlchild As Panel
   Dim btnChild As Button
        
End Sub
Public Sub Initialize(pwidth As Float, pheight As Float, pTag As String, txt As String,pParent As Object)
   pnlchild.Initialize("")
   btnChild.Initialize("Child")
   btnChild.Tag=pTag
   btnChild.Text=txt
   pnlchild.AddView(btnChild, 20, 20, pwidth,pheight)
        oParent = pParent
End Sub
Public Sub MainPanel() As Panel
Return pnlchild
End Sub
Sub Child_Click
Dim b As Button
   b = Sender
   CallSub2(Me , "ChildManager", b.tag)
End Sub

CallSub2(Me , "ChildManager", b.tag) is the problem. You are trying to call the Sub "ChildManger" in the current class because you are using the ME keyword which refers to the current object. When you create the instance of the child class you need to pass a reference to the parent class to it so the code would look like:

B4X:
Sub Class_Globals
   Dim pnlchild As Panel
   Dim btnChild As Button
        'Passed as me from the parent object
        Dim oParent As Object
End Sub
Public Sub Initialize(pwidth As Float, pheight As Float, pTag As String, txt As String,pParent As Object)
   pnlchild.Initialize("")
   btnChild.Initialize("Child")
   btnChild.Tag=pTag
   btnChild.Text=txt
   pnlchild.AddView(btnChild, 20, 20, pwidth,pheight)
        oParent = pParent
End Sub
Public Sub MainPanel() As Panel
Return pnlchild
End Sub
Sub Child_Click
Dim b As Button
   b = Sender
   CallSub2(oParent , "ChildManager", b.tag)
End Sub
 
Last edited:
Upvote 0
Top