Silvia
Silvia

Reputation: 3

doing multiple labels in Visual studio

This is what I have for clicking on one label so it could change color, but I would like to know how to make it so the next labels will also turn red only if clicked. In

In total, I have 48 labels If Seat1.BackColor = Color.White Then Seat1.BackColor = Color.Red Else Seat1.BackColor = Color.White End If

Upvotes: 0

Views: 612

Answers (1)

NoAlias
NoAlias

Reputation: 9193

You can have the same sub routine handle all of the seat label click events by casting the sender parameter as a Label:

Private Sub HandleSeatClick(sender As Object, e As EventArgs) Handles Seat1.Click, Seat2.Click, Seat3.Click

    Dim lblTarget As Label = CType(sender, Label)

    If lblTarget.BackColor = Color.White Then

        lblTarget.BackColor = Color.Red

    Else

        lblTarget.BackColor = Color.White

    End If

End Sub

If all of your seat labels are named in the same way (example = Seat5, Seat6, Seat7, ... , Seat48) then you can leverage AddHandler so you don't have to wire up the 48 labels with the Handles in the HandleSeatClick routine definition:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim intCursor As Integer = 1

    Do Until intCursor = 48

        Dim lblTarget As Label = CType(Me.Controls.Find("Seat" & intCursor.ToString(), False).First(), Label)

        AddHandler lblTarget.Click, AddressOf HandleSeatClick

        intCursor += 1

    Loop

End Sub

Upvotes: 1

Related Questions