Hello to all, i am new in b4a and i'm stuck in something maybe very stupid.
I' have created 2 buttons (with images) using the Designer and added the members:
B4X:
Sub Globals
Dim Button1 As Button
Dim Button2 As Button
End Sub
I created a sub that does a bunch of things once Button1 is clicked, i want to use the same sub for all 2 buttons i've created without duplicate it.
I want to do this because i need to change the value of some variables before execute my function depending of which button is clicked and i don't want to duplicate the sub for every button (which will be over 30)
I tried something like this but no luck:
B4X:
Button1.Initialize("mysub")
Button1.Tag = 1
Button2.Initialize("mysub")
Button2.Tag = 2
Dim btn As Button
btn = Sender 'Cast the Object to Button
Select btn.Tag
Case 1
' Set my vars..
Case 2
' Set my vars..
End Select
Sub mysub_click '<-- I want to use it for all buttons, but passing different values to my vars and without duplicate it.
'do things.
End Sub
There is a good button example withe Sender, in the Beginner Guide. I think the Beginner guide could be called the Essential Guide, great information there.
Hello, thanks! would you be so kind to write an example?
This is working for me, but only if i add the buttons from code, i am using the designer.
Gonna read the guide tonight for sure =)
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("mainl")
Button1.Initialize("Btn")
Button1.Tag = 1
Button2.Initialize("Btn")
Button2.Tag = 2
Activity.AddView(Button1, 10dip, 10dip, 200dip, 50dip)
Activity.AddView(Button2, 10dip, 70dip, 200dip, 50dip)
End Sub
Sub Btn_Click
Dim Btn As Button
Btn = Sender
Select Btn.Tag
Case 1
' Set my vars..
message = "Button1"
Case 2
message = "Button2"
' Set my vars..
End Select
Msgbox(""&message,"")
End Sub