B4J Question Accessing an Array of Buttons

David Hawkins

Active Member
Licensed User
Longtime User
Hi I am creating an array of buttons and trying to individually select the text of a button that has been pressed. I have tried the 'sender' option and that will only show me that last number in the array regardless of which button I click on. I have tried the button tag and the button id nothing work. What am I doing wrong, please help.

The code (it's not the total code) I am using is below

B4X:
    Cntr = 0
   
    offsetY = 125
    offsetX = 40   
    Width = 40
    Height = 30   
   
    Dim Done As Boolean
    Done = False
   
    For y = 0 To DaysInMonth - 1
        If y = 0 Then
            For x = 7 - FirstNode To 6
                Cntr = Cntr + 1
                b.Initialize("b")
                MainForm.RootPane.AddNode(b, offsetX + x * (Width + 5dip), offsetY, Width, Height)
                b.Font = fx.DefaultFont(14)
                b.Text = Cntr
                b.Tag = Cntr
                b.Id = Cntr
            Next
        Else
            For x = 0 To 6
                Cntr = Cntr + 1
                b.Initialize("b")
                MainForm.RootPane.AddNode(b, offsetX + x * (Width + 5dip), offsetY + y * (Height + 5dip), Width, Height)
                b.Font = fx.DefaultFont(14)
                b.Text = Cntr
                b.Tag = Cntr
                b.Id = Cntr
                If Cntr = DaysInMonth Then
                    Done = True
                    Exit
                End If               
            Next               
        End If   
        If Done = True Then Exit
    Next

B4X:
Private Sub b_MouseClicked (EventData As MouseEvent)
    Dim bb As Button
    bb = Sender
    Log(bb.Id)
    msgbox.Show(EventData,"")
End Sub
 

chuck3e

Active Member
Licensed User
Longtime User
Firstly, in order for b_MouseClicked to work the button Id must = b, I believe. Since you're putting numbers in Id then you need Subs called 1_MouseClicked, 2_MouseClicked. etc.
 
Upvote 0

David Hawkins

Active Member
Licensed User
Longtime User
Firstly, in order for b_MouseClicked to work the button Id must = b, I believe. Since you're putting numbers in Id then you need Subs called 1_MouseClicked, 2_MouseClicked. etc.
I think the b as in b.Initialize("b") is the event variable and not the b.id as I tried your suggestion and unfortunately it didn't work.

As the buttons are created dynamically it would prove difficult to create all MouseClicked sub routines dynamically as well.

The Private Sub b_MouseClicked (EventData As MouseEvent) works in as much as the event is triggered and the 'eventdata' shows that the correct id has been selected I just don't seem to be able to pick it up from the 'sender' using b.id.

Regards and thanks for your prompt reply I hope you have a happy new year.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You should dim a new button in each iteration of the loop(s), otherwise you are dealing with the same button each time and just changing the content.
 
Upvote 0

David Hawkins

Active Member
Licensed User
Longtime User
Hi stevel05

dim b as button
b.Initialize("b")

Thank you for your suggestion it worked perfectly.

If you haven't guessed with this last piece of code I have now produced a DateTimePicker for B4J which I will put up on the site shortly.

Regards and thanks for your prompt reply I hope you have a happy new year.

David
 
Upvote 0
Top