Public Class Form1
Inherits System.Windows.Forms.Form
Dim Labels(10) As Label
Public Sub New()
MyBase.New()
SetupLabels()
End Sub
Private Sub SetupLabels()
Dim Looper As Integer
For Looper = 0 To Labels.Length - 1
' Create a new label
Labels(Looper) = New Label
' Setup Label here
Labels(Looper).Text = Looper.ToString()
Labels(Looper).Top = Looper * 25
' Adds a click event handler to the current label
' AddressOf is needed to get the address of the subroutine
AddHandler Labels(Looper).Click, AddressOf LabelsClick
Next
' Adds the controls to the form's ControlCollection
Controls.AddRange(Labels)
End Sub
' This is the event handler for all of the labels:
Sub LabelsClick(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show(CType(sender, Label).Text)
End Sub
End Class