Which button is clicked in button array?

mleffler

Member
Licensed User
Longtime User
I'm writing a program that uses a lot of buttons.
In this example 24. The buttons create correctly.
I am storing the button number in the .tag property.
When I click a button the Msgbox shows a tag value of 24 all the time.

B4X:
Sub CreateBuildingButtons
   Dim MaxXButtons As Int
   Dim MaxYButtons As Int
   Dim XButtonCount As Int
   Dim YButtonCount As Int
   Dim ButtonCount As Int
   Dim ButtonImage As Bitmap
   Dim XButtonPos As Int
   Dim YButtonPos As Int
   Dim BldgCheckButton As Button
   Dim BuildingLabel As Label
      
   'Set beginning button information.
   MaxXButtons = 5
   MaxYButtons = 3
   ButtonCount = 0
   ButtonImage.Initialize(File.DirAssets,"chrome_btn.png")
   XButtonPos = 35
   YButtonPos = 20
   
   For  YButtonCount = 0 To MaxYButtons
   'Create rows
      For XButtonCount = 0 To MaxXButtons
      'Create columns
         
         'Create button
         BldgCheckButton.Initialize("BldgCheckButton")
         BldgCheckButton.SetBackgroundImage(ButtonImage)
         BuildingPanel.AddView(BldgCheckButton,XButtonPos,YButtonPos,170,120)
         BldgCheckButton.Tag = ButtonCount
         
         'Building Name as label
         BuildingButtonName(ButtonCount) = "Building Label"
         BuildingLabel.Initialize("BuildingLabel")
         BuildingLabel.TextSize = 18
         BuildingLabel.TextColor = Colors.Black
         BuildingLabel.Gravity = 1
         BuildingLabel.Text = BuildingButtonName(ButtonCount)
         BuildingPanel.AddView(BuildingLabel,XButtonPos,YButtonPos +10,170,40)
         'Reading as label
         BuildingButtonReading(ButtonCount) = "999.9F"
         BuildingLabel.Initialize("BuildingLabel")
         BuildingLabel.TextSize = 18
         BuildingLabel.TextColor = Colors.Black
         BuildingLabel.Gravity = 0
         BuildingLabel.Text = BuildingButtonReading(ButtonCount)
         BuildingPanel.AddView(BuildingLabel,XButtonPos+10 ,YButtonPos+90,170,40)
         ButtonCount = ButtonCount +1
         XButtonPos = XButtonPos + 210
         BuildingButton(XButtonCount,YButtonCount) = BldgCheckButton
         
      Next
      YButtonPos = YButtonPos + 140
      XButtonPos = 35
   Next
End Sub
Sub BldgCheckButton_Click
   Dim B As Button
   Dim ButNum As String
   
   B = Sender
   ButNum = B.tag
   Msgbox("Text is: " &B.tag," Bldg Button Clicked")
End Sub

Any ideas what is going on? I'm not seeing it.
I modeled this after Erel's Tick Tack Toe example but must have done something wrong.

F.Y.I. I have already written this program in another language where this would be a perfect example of how to use a class. In that language the buttons are defined as classes and then the class is used to make the instances needed.

thanks,
 

lagore

Active Member
Licensed User
Longtime User
The problem is you have not created an array of buttons but in essence the same button 24 times. Declare your button as
B4X:
Dim BldgCheckButton(24) As Button
then replace every occurrence of "BldgCheckButton" with "BldgCheckButton(ButtonCount)" except the one that is the EventName in the initialize statement.
 
Upvote 0

mleffler

Member
Licensed User
Longtime User
It is working now

Thanks. It works fine now.
I actually had the array declared already but I miss understood how it was being used in the Tick Tack Toe example.

Now I can do the other 4 pages of buttons. There are 5 pages of 24 buttons in this program.
 
Upvote 0
Top