Which button clicked

jasemilly

Member
Licensed User
Longtime User
I have created in code three buttons which appear on my view.

My question is how do I know which one has been clicked? I tried using sender and that didn't work or I did wrong?

should my Sub BtnTackleCount_Click

have something to reference the array???

all very new thanks jasemilly.:sign0104:
 

klaus

Expert
Licensed User
Longtime User
How did you initialize the Buttons ?
Do you want one Click event for each Button or one Click event for all three Buttons ?

Code for three event routines :
B4X:
Button1.Initialize("Button1")
Button2.Initialize("Button2")
Button3.Initialize("Button3")
'
'
Sub Button1_Click

End Sub

Sub Button2_Click

End Sub

Sub Button3_Click

End Sub
Code for one event routines :
B4X:
Button1.Initialize("Buttons")
Button1.Tag = 1
Button2.Initialize("Buttons")
Button2.Tag = 2
Button3.Initialize("Buttons")
Button3.Tag = 3
'
'
Sub Buttons_Click
    Dim btn As Button
    btn = Sender

    Select btn.Tag
    Case "1"
        ' code for Button1
    Case "2"
        ' code for Button2
    Case "3"
        ' code for Button3
    End Select
End Sub
Best regards.
 
Upvote 0

jasemilly

Member
Licensed User
Longtime User
Button click

I forgot to say it was a button array


For I = 1 to 3
ButtonClick(I).Initialize("ButtonClick(I)")
next


I Would like this to become a data collection for a team and the buttons are dynamic.

I will just have a button and the players name next too.

I would prefer to have the same amount of buttons as players.

thanks
 
Upvote 0

jasemilly

Member
Licensed User
Longtime User
Button Clicked

Thanks I am capturing which button was clicked I am trying to store the
number of times each button has been clicked using the following code.

in globals

Type indices(y As Int)
Dim mCount(3) As Int

looping through in activity create

Dim i As indices
i.initialize 'Not really necessary in this case
i.y = y
b.Tag = i

Sub button_Click

Dim b As Button
b = Sender
Dim i As indices
i = b.Tag
mCount(i) = mCount(i) +1
b.Text = mCount(i)


End Sub

the error I get numberformatexception invalid double

All I am trying to do is add 1 to the current value of the array.


Thanks for any help
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
First, what type and value is y ?

Why do you use a type for only one variable.

Why dont's you use this code:
B4X:
Dim mCount(3) As Int

' looping through in activity create

b.Tag = y

Sub button_Click
    Dim b As Button
    b = Sender
    Dim i As Int
    i = b.Tag
    mCount(i) = mCount(i) +1
    b.Text = mCount(i)
End Sub
The problem in your code is:
You define i as indices, which is a type variable
and you expect it beeing an integer in mCount(i)?
B4X:
Dim i As indices
    i = b.Tag
 mCount(i) = mCount(i) +1
You code should look like this:
B4X:
Dim i As indices
    i = b.Tag
 mCount(i.y) = mCount(i.y) +1
Best regards.
 
Upvote 0

jasemilly

Member
Licensed User
Longtime User
Got it

Thanks

that got it using your suggesting, it was along that way I wanted to do it, but came across the indicies.

Y is a int that just counts through the loop.

jasemilly
 
Upvote 0
Top