On a WinForm, I have 84 labels, and I want to determine which of the 84 labels incurred a MouseDown event. I know I can code a MouseDown event handler for each of the 84 labels, but that is a lot of unnecessary code when they all do about the same thing in different locations on the form.
How do I code a generalized Sub or Function that detects a MouseDown event and provides the Label.Name on which it fired? I populate the labels on the form with a generalized routine that gets the Label.Name, so I can work with it. A copy of it follows:
Private Function FillLabelTextInMeControls(ByVal labelName As String, _
ByVal textValue As String) As Boolean
label = New Label
For Each labelFound As Label In Me.Controls.OfType(Of Label)()
If Convert.ToString(labelFound.Name) = labelName Then
label = labelFound
'label.ForeColor = Color.Red
label.Text = textValue
label.Visible = True
Return True
Exit Function
End If
Next
Return False
End FunctionTerry 01









