array out of bound exception

croccio

Member
guy i've this error and i don't know why :(
on this line
pnlcontenitore.AddView(quadrato(i), 0+percento(3, False), 0+percento(3, False), latoquadrato, latoquadrato)
here the code

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim pnlContenitore As Panel
   Dim quadrato(0 To 9) As Button
   Dim altezza, larghezza As Long
   Dim latoQuadrato As Double
   Dim nQuadrati As Int
   Dim Button As Button
   nQuadrati=8
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim layout As LayoutValues
   Dim i As Int
   activity.LoadLayout("general")
   altezza=layout.Height
   larghezza=layout.Width
   
   pnlcontenitore.Height=larghezza
   pnlcontenitore.Width=larghezza
   pnlcontenitore.Top=(altezza/2)-(pnlcontenitore.Height/2)
   
   latoquadrato=(larghezza/3)-percento(12, False)
   For i=0 To nquadrati
      Select Case i
         Case 0, 3, 6
            pnlcontenitore.AddView(quadrato(i), 0+percento(3, False), 0+percento(3, False), latoquadrato, latoquadrato)
         Case 1, 2, 4, 5, 7, 8
            pnlcontenitore.AddView(quadrato(i), quadrato(i-1).Left, quadrato(i-3).width+percento(3, False), latoquadrato, latoquadrato)
      End Select
   Next
End Sub

percento is a function that i made

how can i create an array of button from designer?
if i have to create array from code (as the code above) of can i do events onclick of a button(4) for example?
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Unfortunately you dont give enough information.
- You dim 10 objects but you use only 9 ?
- Dim quadrato(0 to 9) As Button, is not allowed, you should use Dim quadrato(9) As Button, for 9 objects.
- As your Buttons are defined and added in the code you must initialize the Buttons before you add them with the same EventName.
- Then you should set the Tag property to i
- Then, as all Buttons have the same EventName, you can have one Click event for all Buttons and get the Button that raised the event with the Sender keyword.

You cannot set an array of Buttons in the Designer.

Anyway it would be mucheasier if you posted your project as a zip file so we could make the changes and test them.

Best regards,
 
Upvote 0
Top