Page 1 of 1

Combining Event Handlers

Posted: Tuesday 31st August 2021 11:09pm
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.

Re: Combining Event Handlers

Posted: Wednesday 1st September 2021 3:32am
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.)

Re: Combining Event Handlers

Posted: Wednesday 1st September 2021 1:50pm
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