3

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 Answers6

6
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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

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
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • What will I have to do? Thanks. – Newton Isaac Apr 22 '14 at 16:11
  • Thanks you. But I want to get the index of Labels, how to do this? – Newton Isaac Apr 22 '14 at 16:19
  • How are you adding your labels in the first place? At run time (as I've shown above) or at design time? What index are you referring to? – rory.ap Apr 22 '14 at 16:23
  • Thanks, I have n Labels ready on my Form (default is Label1, Label2,..., Labeln), I create them at design-time, the index of them is my imagination for them! Label1->1; Label2->2;...; Labeln->n. – Newton Isaac Apr 22 '14 at 16:27
  • Okay, you could still simplify things by coding the handlers like I showed you. To get your index, use `Substring` in your event handler: `MsgBox(DirectCast(sender, Label).Name.Substring(5))`. However, this assumes that you've set the name of your label using the `.Name` property. You can't refer to the instance name itself anywhere (see this for reason why: http://stackoverflow.com/questions/10205427/get-the-instance-name-of-the-object-no-the-object-type-name-in-c-sharp-4-0). – rory.ap Apr 22 '14 at 16:39
2

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.

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
0

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?

llc381
  • 101
  • 2
  • 10
0

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
shadow
  • 1,883
  • 1
  • 16
  • 24
0

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