Android Question Problem with array button click

Discussion in 'Android Questions' started by juniorteacher, Jun 24, 2015.

  1. juniorteacher

    juniorteacher Member Licensed User

    error in sender why?

    Code:
    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(64As 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(64As 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
     
  2. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Try this...

    Code:
    Sub buattbl
    Dim k,j,y As Int
    Dim but(64As 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.
     
    lemonisdead and DonManfred like this.
  3. juniorteacher

    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)
     
  4. juniorteacher

    juniorteacher Member Licensed User

    this my project,
     

    Attached Files:

  5. DonManfred

    DonManfred Expert Licensed User

    Sure. You want to cast ONE object to an array. This is the wrong way of using the "sender"

    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.
     
    lemonisdead likes this.
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Let me try to explain more clearly now that I am at my PC.

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


    Code:
    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.
    Code:
    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...
    Code:
    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.
     
    DonManfred likes this.
  7. juniorteacher

    juniorteacher Member Licensed User

    thanks for the answer,
    Code:
    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,?
     
  8. qsrtech

    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....
     
  9. juniorteacher

    juniorteacher Member Licensed User

    the real problem is identify button with button.tag :D
     
  10. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Sorry you are correct, it was a typo and should have been but(63)
    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.
     
  11. juniorteacher

    juniorteacher Member Licensed User

    my problem solved, thanks for all answer.


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

    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...
    Code:
    Sub But_Click
      
    Dim but As Button = Sender
      but.Visible = 
    False       
    End Sub
    Working example attached.
     

    Attached Files:

    lemonisdead and DonManfred like this.
Loading...