b4a v2Beta - Can a class module to create an event?

Petrus

Member
Licensed User
Longtime User
Can a class module to create an event? Example of a User View "cbButton":

Class module "cbButton":
B4X:
Sub Class_Globals
   Dim Control As Object
   Private Panel1 As Panel
   Private Label1 As Label
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize()
   Panel1.Initialize("Panel1")
   Control = Panel1
   Panel1.Color = Colors.Magenta
   Label1.Initialize("")
   Label1.TextColor = Colors.Black
   Label1.TextSize = 20
   Label1.Text = "Test"
   Label1.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
   Panel1.AddView(Label1, 0, 0, 10,10)
End Sub

Public Sub SetLayout(Left As Int, Top As Int, Width As Int, Height As Int)
   Panel1.SetLayout(Left, Top, Width, Height)
   Label1.SetLayout(0,0,Width, Height)
End Sub

Sub Panel1_Click()
   'and now?
   '...
End Sub

Activity module:
B4X:
Sub Globals
   Dim CbButton1 As cbButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
   CbButton1.Initialize()
   Activity.AddView(CbButton1.Control, 10,10,100,50)
   CbButton1.SetLayout(100, 100, 200, 100)
End Sub

Sub CbButton1_Click()
   '????
   
End Sub

Greetings
Petrus
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes, it can raise events with CallSub keywords.

For example here is an EditText that raises an event when the user writes 123:
B4X:
'Class MyEditText module
Sub Class_Globals
   Private et As EditText
   Private module As Object
   Private eventName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize (ModulePar As Object, EventNamePar As String)
   module = ModulePar
   eventName = EventNamePar
   et.Initialize("et")
   et.InputType = et.INPUT_TYPE_NUMBERS
End Sub

Sub AsView As View
   Return et
End Sub

Sub et_TextChanged (Old As String, New As String)
   If New = "123" Then
      CallSub(module, eventName  & "_Match")
   End If
End Sub

'***********************************
'Main module
Sub Globals
   Dim txt As MyEditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   txt.Initialize("Main", "txt")
   Activity.AddView(txt.AsView, 10dip, 10dip, 200dip, 50dip)
End Sub

Sub txt_Match
   Msgbox("Match!", "")
End Sub
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
I use the exact same method to call such a "fake" event sub in my activity. I wanted to check if the sub in the activity exists but this seems not to be possible because SubExists() can only be used with user classes?

Would be nice if SubExists() have a similar signature like CallSub() so you can write

B4X:
if SubExists("Main", "MySub") then
   CallSub("Main", "MySub")
end if

and check if a sub exists in an activity module.
 
Upvote 0

Petrus

Member
Licensed User
Longtime User
Hi Erel, Thanks for the quick response!
Can I pass the values to the event? (in the example: Old As String, New As String)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two related changes in the next beta update:
1. SubExist will work with services and activities as well.
2. Me keyword can be used to pass a reference to an activity module or service module. This will be only useful for CallSub related keywords. So instead of passing the module name as string you will be able to pass Me.
 
Upvote 0
Top