Combining Event Handlers

Ask about the individual Gambas components here.
Post Reply
User avatar
sadams54
Posts: 139
Joined: Monday 9th July 2018 3:43am
Contact:

Combining Event Handlers

Post by sadams54 »

Sorry to be asking so much however I am wondering if there is a way to make several controls all fire the same handler.

For example, I have 3 buttons named... btnDoSomething1 btnDoAnything1 btnDoSomething2

I want them all to fire the event btnDoSomething1_click rather than each have it's own click event.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Combining Event Handlers

Post by BruceSteers »

sadams54 wrote: Tuesday 31st August 2021 11:09pm Sorry to be asking so much however I am wondering if there is a way to make several controls all fire the same handler.

For example, I have 3 buttons named... btnDoSomething1 btnDoAnything1 btnDoSomething2

I want them all to fire the event btnDoSomething1_click rather than each have it's own click event.
Sure just set the Group

If they all have the Group "MyButts"
then MyButts_Click will be useable.

(the IDE will auto-goto event handler on double clicking if a group is set.)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Combining Event Handlers

Post by cogier »

Run this code in a Graphical Program. Hopefully it will help you.
hButton As Button
Label1 As Label

Public Sub AllButtons_Click()

  Label1.Text = Last.Name

End

Public Sub Form_Open()

  ''All this is to add components to the Form
  Dim sButtonNames As String[] = ["btnDoSomething1", "btnDoAnything1", "btnDoSomething2"]
  Dim iLoop As Integer

  With Me
    .Height = 215
    .Width = 215
    .Padding = 5
    .Arrangement = Arrange.None
    .Center
  End With

  For iLoop = 0 To 2
    With hButton = New Button(Me) As "AllButtons"
      .Y = iLoop * 50
      .X = 20
      .W = 175
      .H = 28
      .Name = sButtonNames[iLoop]
      .Text = sButtonNames[iLoop]
    End With
  Next

  With Label1 = New Label(Me) As "Label1"
    .Y = iLoop * 50
    .X = 20
    .W = 175
    .H = 28
    .Alignment = Align.Center
    .Font.Bold = True
    .Border = Border.Plain
  End With

End

Post Reply