[Gambas-user] how to place code into events of an array of buttons?
Bruce Steers
bsteers4 at gmail.com
Tue Feb 1 18:41:31 CET 2022
On Tue, 1 Feb 2022 at 15:57, <roberto.premoli at tiscali.it> wrote:
> Usually i create object one by one using graphila placemen of object,
> but now i need a lot of buttons, label, ecc in my aplication,
> so i create them "in code" to have an array of them.
> here ad example with only 5 buttons:
>
> Private btns[5] As Button
> Private matrice_testo[5] As String
>
> Public Sub Form_Open()
> Dim larghezza As Integer
> Dim altezza As Integer
> Dim larghezza_tasto As Integer
> Dim altezza_tasto As Integer
> Dim n, m As Integer
> Dim offset_verticale As Integer
>
> larghezza = fmain.Width
> altezza = fmain.height
> larghezza_tasto = larghezza / 10
> altezza_tasto = altezza / 12
> offset_verticale = altezza / 2
>
> For n = 0 To 4
> btns[n] = New Button(FMain) As "btns"
> Next
>
> 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 'prima riga'
> btns[n].text = matrice_testo[n]
> btns[n].Left = larghezza_tasto * n
> btns[n].Top = offset_verticale
> btns[n].Height = 50
> btns[n].width = larghezza_tasto
> btns[n].Show
> Next
>
> end
>
> the code place the buttons as I want but now i don't know how to place
> code inside the "click", doubleclick", "got focus" ecc and other events of
> the buttons. Can someone tell me how?
> many thanks.
> Roberto
>
Here's how i would do it....
Notes.
no need to create a btns[] array you can access the details of the button
in the Click handler using Last
no need to set X,Y, width, height if you place buttons in a HBox or other
arranged panel
Form_Open()
FMain.Arrangement = Arrange.Vertical
Dim btns as Button ' note. not a global variable
Dim hb As HBox
hb = New HBox(FMain) ' This makes a horizontal box, any buttons
placed inside will be arranged horizontally so no need to set their
X,Y positions or size
hb.Height = 50 ' HBox width will be automatically expanded in a
vertically arranged container.
hb.Spacing = True
Dim aNames as String[] = ["A","B","C","D","E"]
For n = 0 To aNames.Max
btns = New Button(hb) As "btns" ' add buttons to HBox
btns.Text = aNames[n]
btns.Name = aNames[n]
btns.AutoResize = True ' maybe use .Expand = True if you want all
buttons to stretch
Next
End
Public Sub btns_Click()
Print Last.Name;; "was clicked"
End
Alternatively set an Action key (string), you can then control what happens
in the Actions_Activate event....
Public Sub Form_Open()
Dim btns as Button ' note. not a global variable
Dim hb As HBox
hb = New HBox(FMain) ' This makes a horizontal box, any buttons
placed inside will be arranged horizontally so no need to set their
X,Y positions or size
hb.Height = 50 ' HBox width will be automatically expanded in a
vertically arranged container.
hb.Spacing = True
Dim aNames as String[] = ["A","B","C","D","E"]
For n = 0 To aNames.Max
btns = New Button(hb) As "btns" ' add buttons to HBox
btns.Text = aNames[n]
btns.Action = aNames[n]
btns.AutoResize = True ' maybe use .Expand = True if you want all
buttons to stretch
Next
End
Public Sub Actions_Activate(Name As String) As Boolean
Select Name
Case "A"
' do A stuff
Case "B"
' Do B stuff
End Select
End
Hope that helps.
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220201/230d6a3a/attachment-0001.htm>
More information about the User
mailing list