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

Bruce Steers bsteers4 at gmail.com
Tue Feb 1 19:08:17 CET 2022


On Tue, 1 Feb 2022 at 17:56, Bruce Steers <bsteers4 at gmail.com> wrote:

>
>
> On Tue, 1 Feb 2022 at 17:51, <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 it is not possible, i can populate ".tag = n" and read it but it
>> will appear
>> to me more "clean" to use ".index" or similar, if available.
>> Roberto
>>
>> PS: i miss so much to have "indexed objects" in Gambas graphical
>> interface as it
>> is available in VB.
>>
>>
>> Con Tiscali Mobile Smart 70 hai 70 GB in 4G, minuti illimitati e 100
>> SMS a soli 7,99€ al mese http://tisca.li/Smart70
>>
>>
>>
>> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>>
>
>
> This will work if your buttons are all in one parent container
>
>
> Public Sub GetButtonIndex(btn As Button) As Integer
>
> Dim b As Button, i As Integer
> For i = 0 To btn.Parent.Children.Count - 1
>   if btn.Parent.Children[i] = btn Then Return i
> Next
>
> End
>

oops,, forget the b as Button lol.
if the parent container is not just buttons you can do this...

Dim o As Object, i As Integer
For Each o In btn.Parent.Children
  If Object.Type(o) <> "Button" Then Continue
  if o = btn Then Return i
  Inc i
Next

But then you could always just enumerate the button names or use the Tag
like you say.

Wishing well
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220201/62329930/attachment.htm>


More information about the User mailing list