Another solution to achieve described functionality (without
List(of T)
) is to create custom class
MyButton
with
ClickOrder
and
ClickedCount
properties.
Hot to do it in VB.NET?
1) Add new class (set the name of file to
MyButton.vb)
Public Class MyButton
Inherits Button
Dim iCounter As Integer = 0
Dim co As Integer = 0
Property ClickOrder() As Integer
Get
Return co
End Get
Set(ByVal value As Integer)
co = value
End Set
End Property
ReadOnly Property ClickedCount() As Integer
Get
Return iCounter
End Get
End Property
Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
iCounter += 1
End Sub
End Class
2) catch click event and view some information
Form1.vb
Public Class Form1
Private WithEvents btn As MyButton
Dim i As Integer = 0
Public Sub New()
InitializeComponent()
Dim i As Integer = 0
For i = 0 To 9
btn = New MyButton
With btn
.Name = "button" & Int(i + 1).ToString
.Text = .Name
.Parent = Me
.Left = 8
.Top = 24 * i + 4
.Width = 75
.Height = 24
AddHandler btn.Click, AddressOf btn_Click
End With
Next
End Sub
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
Dim b As MyButton = sender
i += 1
b.ClickOrder = i
MsgBox(b.Name & " has been clicked: " & b.ClickedCount.ToString & " time(s)." & vbCr & _
"Click order: " & b.ClickOrder.ToString, MsgBoxStyle.Information, "Message...")
End Sub
End Class