Combining Event Handlers

Ask about the individual Gambas components here.
Post Reply
sadams54
Posts: 83
Joined: Monday 09th July 2018 3:43am

Combining Event Handlers

Post by sadams54 » 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.

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: Combining Event Handlers

Post by BruceSteers » Wednesday 01st September 2021 3:32am

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.)
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 757
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Combining Event Handlers

Post by cogier » Wednesday 01st September 2021 1:50pm

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