Matt Bartlett
Matt Bartlett

Reputation: 360

Have multiple ComboBox's use the same event

I have roughly 140 ComboBox on a form. When some selects a new item within one of the box's I would like to highlight that box.

I would like to use the SelectionChangeCommitted event and I would use the following code:

Private Sub cmbDesk1_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cmbDesk1.SelectionChangeCommitted
 Dim tbControl As ComboBox = DirectCast(sender, ComboBox)
 tbControl.BackColor = Drawing.Color.Red
 tabFloor1.Focus()

I know that I can add extra lines after the "Handles" section of the subroutine declaration but it there a neat way of doing it without adding every ComboBox to it? Something similar to

Me.TabPage1.Controls.OfType(Of ComboBox)()

Kind regards

Matt

Upvotes: 1

Views: 667

Answers (1)

the_lotus
the_lotus

Reputation: 12748

You can always loop all combobox and AddHandler yourself.

For Each cb As ComboBox In Me.TabPage1.Controls.OfType(Of ComboBox)()
    AddHandler cb.SelectionChangeCommitted, AddressOf cmbDesk1_SelectionChangeCommitted
Next

This should be done once.

Upvotes: 4

Related Questions