Italian Array di moltissimi pulsanti

AlpVir

Well-Known Member
Licensed User
Longtime User
Per creare molti pulsanti gestibili da una unica Sub BotMappa_Click utilizzo abitualmente il seguente codice

B4X:
Dim BotMappa(13)  As Button
Dim b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13 As Button

BotMappa = Array As Button (b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13)
for i=0 to 12
    BotMappa(i).Initialize ("BotMappa")
    Activity.addview (BotMappa(i), ecc ecc ecc
next

Ma, ed è questa la domanda, se i pulsanti sono (ad esempio) 200 devo scrivere
b14,b15,b16,b17,b18,b19 .... fino a 200 ? Non c'è qualche scorciatoia ?

Grazie per l'attenzione.
 

klaus

Expert
Licensed User
Longtime User
You don't need
Dim b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13 AsButton
BotMappa = ArrayAsButton (b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13)


Just
B4X:
Dim BotMappa(13)  As Button

For i=0 to 12
    BotMappa(i).Initialize ("BotMappa")
    Activity.AddView (BotMappa(i), ecc ecc ecc
    BotMappa(i).Tag = i
Next
'
'
Sub BotMappa_Click
    Dim btn As Button
    Dim Index As Int

    btn = Sender      ' button that raised the ecent
    Index = btn.Tag  ' index of the button that raised the event
    '  your code
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Se è soltanto per gestire l'evento Click, è sufficiente:
B4X:
for i=0 to 12
    Dim btn As Button
    btn.Initialize ("BotMappa")
    Activity.addview (btn, ecc ecc ecc
next
 

LucaMs

Expert
Licensed User
Longtime User
Ricordati che le List (o le Map) sono migliori degli Array perché puoi aggiungere/eliminare (ordinare) elementi a piacimento, cosa non semplice con gli Array
 

AlpVir

Well-Known Member
Licensed User
Longtime User
Intuivo che ci dovesse essere un modo migliore per risolvere la questione, ma non riuscivo a trovarlo.
Grazie a tutti.
 
Top