Control & Sender

copiloto

Member
Licensed User
Longtime User
Hello,

I'm trying to create and capture click events in B4a from a list of five buttons programatically added (not designer mode): The program objetive is simple: Write into clicked button a random number

....

for a=1 to 5
activity.addview(????)
next

.....

Sub Button_Click ' tries to capture a click event of whichever button clicked... fail..
Dim b as button
b.text=rnd(1,50)
End Sub

Please, could someone explain me how to implement it as easy as possible? I have read some examples but are not what I need (not sure... be patient, please;) )

Thanks a lot.
 

lawboy

Member
Licensed User
Longtime User
Hi,
I think you may need to download the reflection library and add it into your additional libraries folder then run the demo. I am new here so don't hold me to it.

The Following code will change the button text when clicked:
B4X:
Sub Globals
   Dim Obj1 As Reflector
   Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Layout")
End Sub

Sub Button1_Click   
   Obj1.Target = Button1
   Obj1.RunMethod2("setText", "I've been" & CRLF & "Pressed!", "java.lang.CharSequence")
End Sub

Reflection Library:
http://www.b4x.com/forum/additional-libraries-official-updates/6767-reflection-library.html

I hope this helps you,
Lawboy
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
No the Reflection library is not necessary.
You must initilize your buttons with:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Button1.Initialize("Buttons") ' Buttons is the EveneName, the same for all buttons[
  Button2.Initialize("Buttons")
  Button3.Initialize("Buttons")
  Button4.Initialize("Buttons")
  Button5.Initialize("Buttons")

  Activity.AddView(Button1,0,0,60,60)
  Activity.AddView(Button2,60,0,60,60)
  Activity.AddView(Button3,120,0,60,60)
  Activity.AddView(Button4,180,0,60,60)
  Activity.AddView(Button5,240,0,60,60)
End Sub

And then one routine for all buttons.
B4X:
Sub Buttons_Click
  Dim btn [As Button
  btn = Sender  ' sets the buuton that raised the event to btn
  btn.Text=Rnd(1,50)
End Sub

Best regards.
 
Last edited:
Upvote 0

copiloto

Member
Licensed User
Longtime User
Thanks very much lawboy and klaus for your answers.

About the library, lately, I haven't had time enought to have a watch to them. Even I haven't had time to update new release/update of B4a. I recognize that lots of times, power of libraries simplifies life considerably.. I'll consider your suggested libray for future projects.

klaus, is there any way of generalize your solution? I mean, if I wish to create a lot of buttons, for example 50, 80, 150 ... could it be initialized such as: button & i.initialize("Buttons") and addView(Button & i) ?
Solution of creating button by Erel is OK. But I don't know how to identify the button that was clicked. In minesweapper game, you click on one of them and it appears a number, an empty cell or a bomb. I would like to reproduce that game.

Best regards!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try this code:
B4X:
Sub Globals
  Dim Map1 As Map
End Sub[
Sub Activity_Create(FirstTime As Boolean)[
  Dim i, col, row As Int

  Map1.Initialize
  For i = 0 To 14
  Dim btn As Button
  btn.Initialize("Buttons")
  Map1.Put("Button" & i, btn)
  btn.Tag = "Button" & i
  col = i Mod 5
  row = Floor(i/5)
  Activity.AddView(btn, col * 60dip, row * 60dip, 55dip, 55dip)
  Next
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub GetButtonFromName(Name As String) As Button
  Dim b As Button
  b = Map1.Get(Name)
  Return b
End Sub
Sub Buttons_Click
  Dim btn As Button
  btn = Sender  ' sets the buuton that raised the event to bnt
  btn.Text = Rnd(1, 50)
  Msgbox(btn.Tag & " clicked", "Buttons")
End Sub
Best regards.
 
Last edited:
Upvote 0

copiloto

Member
Licensed User
Longtime User
Klaus, it's a great code!!!

I'm very grateful to you!! I'll study your code quietly because there are some commands which haven't experiment yet like 'put' and 'map'. I think that this means to be a structure saving objects... analyze tomorrow.

By the way, only 30 lines of code and you synthesized a great part of the minesweeper game essence. Awesome! Congratulations to you and to B4a for making it possible.

Best regards!
 
Upvote 0
Top