3
votes

I have n Labels on Form, e.g: Label1, Label2,..., Labeln. Normally, when I write Click event for all Labels:

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Msgbox "1"
    End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    Msgbox "2"
End Sub

Private Sub Labeln_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Labeln.Click
    Msgbox "n"
End Sub

Writting is very complex when n is large!

Now, I want to write the code simply to click to Lablei and generate "i" (one proceduce to many proceduces). How to handle it? Thanks in advance.

6
That’s what arrays are for. Do you know how to use them?Konrad Rudolph
Handles Label2.Click, Label3.Click, Labelx.Click, Labely.Click.... extend one of the click events to handle them allŇɏssa Pøngjǣrdenlarp
Thanks, can you help me do specifically?Newton Isaac
Use a ListBox or ListView instead.Hans Passant
Oh, no, by my reason I cant use other controls to instead. Thanks.Newton Isaac

6 Answers

6
votes
Private Sub Labeln_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
       Handles Label1.Click, Label2.Click, Label3.Click '...
    Dim l As Label = DirectCast(sender, Label)
    Msgbox l.Name
End Sub

If n is very large, skip the Handles portion of the method and do this in your form load:

For Each l As Label in Me.Controls.OfType(Of Label)()
    AddHandler l.Click, AddressOf Labeln_Click
Next
2
votes

You can dynamically assign the event handlers when you create your labels using AddHandler:

Sub test()
    Dim label1 As New Label()
    AddHandler label1.Click, AddressOf HandleLabelClick
    Me.Controls.Add(label1)
End Sub

Here's the event handler:

Sub HandleLabelClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MsgBox(DirectCast(sender, Label).Name)
End Sub
2
votes

I would not use the handler and instead make a custom control that inherits from the framework; it would have this message box as a standard feature of the control.

Create a new class as such:

Public Class MyLabel : Inherits Label
    Protected Overrides Sub OnClick(e As EventArgs)
        MyBase.OnClick(e)

        MsgBox(Me.Name)
    End Sub
End Class

Once compiled, it will show up in your toolbox. Paint it on the screen and see it work.

0
votes

Per other answers, you would do best to explicitly add a handler. If the labels are added at runtime (dynamically added at server), you can attach to the AddHandler definition. If the controls are built at design time (defined in markup), handle the OnClick event of each to the same handler. Alternately, for controls built at design time, you can chain each control to the Handles definition of one method.

To get the numerical value, you could use Substring as long as the IDs are, at minimum, of consistent on the text part. Or, you could use regex.match as long as there are no other numbers in the name. I like to append numerical portions of IDs to the end of the ID following an underscore. This allows me to not care what the text portion is, use .split("_"), and gives me the number. This also allows me to append multiple number values with multiple underscores in the event I am using them to identify an ancestor - descendent relationship of objects related to the control (i.e. datatables used to create and fill the control).

You have many options, most of them being equal. Have you found a solution to fit your specific needs yet?

0
votes

You can write something like that:

Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click    
  messagebox.show(directcast(sender, label).text)        
end sub
0
votes

Try this:

Dim i As Integer
Private Sub Form1_Load() Handles MyBase.Load
    For Each l As Label In Me.Controls.OfType(Of Label)()
        AddHandler l.Click, Sub()
                                i = CInt(l.Name.Replace("Label", ""))
                                MsgBox(i)
                            End Sub
    Next
End Sub