[Gambas-user] how to place code into events of an array of buttons?

Bruce Steers bsteers4 at gmail.com
Tue Feb 1 22:36:29 CET 2022


On Tue, 1 Feb 2022 at 18:47, T Lee Davidson <t.lee.davidson at gmail.com>
wrote:

> On 2/1/22 12:50, roberto.premoli at tiscali.it wrote:
> >> Change Form_Open like this
> >> ' ...
> >> matrice_testo[0] = "A"
> >> matrice_testo[1] = "B"
> >> matrice_testo[2] = "C"
> >> matrice_testo[3] = "D"
> >> matrice_testo[4] = "E"
> >>
> >> For n = 0 To 4
> >> With btns[n] = New Button(FMain) As "btns"
> >> .Name = matrice_testo[n]
> >> End With
> >> Next
> >> ' ...
> >>
> >> Public Sub btns_Click()
> >> Print Last.name
> >> End
> >>
> >> Regards
> >> Gianluigi
> >
> > Thanks Gianluigi!
> > there is a way to take also the index of button pressed?
> > because to have the index of button is necessary in other part of the
> program,
> > I tried - naively - to do "variabile = Last.index" bot of course it does
> not work.
>
> If your matrice_testo and btns arrays are always kept in sync, you could
> use the String[].Find method; eg.
> iIndex = matrice_testo.Find(Last.Name)
>
> Personally, I think I'd simply populate the button's Tag with its index.
>

Thinking about it if you've made the global btns[] array just that would
suffice for Find() in a Click event using Last.


Public Sub btns_Click()

   Dim iIndex As Integer = btns.Find(Last)

End

Job done :)
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220201/b7537982/attachment.htm>


More information about the User mailing list