Android Question Problem with array button click

juniorteacher

Member
Licensed User
error in sender why?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("game1")
    buattbl
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub buattbl
Dim k,j,y As Int
For x = 0 To 7
    For y = 0 To 7
        Dim but(64) As Button
        k = ((y*8)+x)
        but(k).Initialize("But")
        panel1.AddView(but(k),9.5%x + (x*10.2%x),14.5%y + (y*7.8%y),10.3%x,7.8%y)
        but(k).Tag = (k)

    Next
Next
End Sub

Sub But_Click
Dim but(64) As Button
but(64) = Sender
Dim btnid As Int = but(64).Tag
   

    Select a
    Case 0
    b = btnid
    a = a + 1
    but(b).Visible = False
    Case 1
    c = btnid
        If b = c + 32 OR c = b + 32 Then
        ToastMessageShow("anda benar",False)
        but(b).Visible = False
        but(c).Visible = False
        Else
        ToastMessageShow("anda salah",False)
        but(b).Visible = True
        but(c).Visible = True
        End If
    a = 0
    End Select
   
       
       
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Try this...

B4X:
Sub buattbl
Dim k,j,y As Int
Dim but(64) As Button
K=0
For x = 0 To 7
    For y = 0 To 7
        but(k).Initialize("But")
        panel1.AddView(but(k),9.5%x + (x*10.2%x),14.5%y + (y*7.8%y),10.3%x,7.8%y)
        but(k).Tag = (k)
        K=k+1

    Next
Next
End Sub

Sub But_Click
Dim but As Button = Sender
Dim btnid As Int = but.Tag
  

    Select a
    Case 0
    b = btnid
    a = a + 1
    but(b).Visible = False
    Case 1
    c = btnid
        If b = c + 32 OR c = b + 32 Then
        ToastMessageShow("anda benar",False)
        but(b).Visible = False
        but(c).Visible = False
        Else
        ToastMessageShow("anda salah",False)
        but(b).Visible = True
        but(c).Visible = True
        End If
    a = 0
    End Select
  
      
      
End Sub
Done on my tablet so there could be mistakes but hopefully you see the idea.
 

juniorteacher

Member
Licensed User
Dim but As Button = Sender (not array)

but(b).Visible = False ( i want to array)

if i write like this => Dim but(64) As Button = Sender (get error)
 

DonManfred

Expert
Licensed User
if i write like this => Dim but(64) As Button = Sender (get error)
Sure. You want to cast ONE object to an array. This is the wrong way of using the "sender"

Sub But_Click
Dim but As Button = Sender
Dim
btnid As Int = but.Tag
is the correct way to get the clicked buttons Tag.

Maybe you need to explain what exactly you want to to when you click on a button.
 

RandomCoder

Well-Known Member
Licensed User
Let me try to explain more clearly now that I am at my PC.

B4X:
Dim but(64) As Button
K=0
Declares your array of Buttons and initialises k which is used as the index pointer into the array.


B4X:
For x = 0 To 7
    For y = 0 To 7
        but(k).Initialize("But")
        panel1.AddView(but(k),9.5%x + (x*10.2%x),14.5%y + (y*7.8%y),10.3%x,7.8%y)
        but(k).Tag = (k)
        K=k+1

    Next
Next
Loops through all 64 Buttons, I'm guessing that your creating a grid of Buttons. This is your original code all that I adjusted was k. On the first iteration k=0 and so we initialise but(0) and add the view to the Panel along with setting the Tag value to 0. The important bit that I added was at the end of the inner For Next loop k=k+1 this now means that we point to but(1) on the next iteration, then 2 and 3 and so on.


You have assigned the same event to all buttons and so the following sub handles any but(0) to but(64) click event. Only one Button can be clicked at any point in time and handled using this sub.
B4X:
Sub But_Click
Dim but As Button = Sender
Now "but" holds a reference to which ever button was pressed whether it be but(0) or but(23) etc. You do not necessarily need to read the tag value and then point directly to the actual Button as done here...
B4X:
Dim btnid As Int = but.Tag

    Select a
    Case 0
    b = btnid
    a = a + 1
    but(b).Visible = False
Instead you could just use but.Visible=False and achieve the same thing.

However you are clearly trying to index another Button in the Array with "but(c)" and I did not know where "a" originated from or your intention at this point which is why I left your original code intact. To be honest it doesn't make a great deal of sense to me as "but(b).Visible" and "but(c).Visible" both appear to reference the same Button?

I have not downloaded your code so this may well have changed since your first post. Hopefully what I have written has helped to explain how Sender is referencing the actual Button that was clicked.
 

juniorteacher

Member
Licensed User
thanks for the answer,
B4X:
Sub But_Click
Dim but As Button = Sender
Dim btnid As Int = but.Tag
 

    Select a  ' i make variable with a
    Case 0
    b = btnid 'save variable b with first click button 
    a = a + 1 'change value for a
    but(b).Visible = False 'i make first click button hidden
    Case 1
    c = btnid 'save variable c with second click button
        If b = c + 32 OR c = b + 32 Then 'check value for game
        ToastMessageShow("anda benar",False) 'if true then second button disappear
        but(b).Visible = False
        but(c).Visible = False
        Else
        ToastMessageShow("anda salah",False) 'if false then first button appear again
        but(b).Visible = True
        but(c).Visible = True
        End If
    a = 0
    End Select
 
     
     
End Sub
can i locate my first click button from button tag,?
 

qsrtech

Active Member
Licensed User
I'm using my phone so i can't verify this but aren't you referencing an invalid object? If i'm correct, your dim(64) creates elements 0..63 and then you're trying to assign to 64 which is actually the 65th element which is out of range....
 

RandomCoder

Well-Known Member
Licensed User
I'm using my phone so i can't verify this but aren't you referencing an invalid object? If i'm correct, your dim(64) creates elements 0..63 and then you're trying to assign to 64 which is actually the 65th element which is out of range....
Sorry you are correct, it was a typo and should have been but(63)
You have assigned the same event to all buttons and so the following sub handles any but(0) to but(64) click event. Only one Button can be clicked at any point in time and handled using this sub.
the real problem is identify button with button.tag :D
Sender returns a refernence to the actual button that was clicked and so yes you can then get the Tag value of that button as shown in the posts above.
 

juniorteacher

Member
Licensed User
my problem solved, thanks for all answer.


i use this code to identify my button
B4X:
For i = 0 To panel1.NumberOfViews - 1
       Dim v As View
        v = panel1.GetView(nobtn)
           v.Visible = False
    Next
 

RandomCoder

Well-Known Member
Licensed User
I do not understand why you have gone to such length? This will hide the button just the same...
B4X:
Sub But_Click
  Dim but As Button = Sender
  but.Visible = False       
End Sub
Working example attached.
 

Attachments

Top