我试图了解为什么我的 AddHandler 不起作用。
如果按钮总是在同一个表单上,但将来可能不会,我找到了一种解决方法。
我也在创建这些按钮,所以我可以添加几个变量供以后使用
有没有人给我一个简单的答案?
谢谢马克
形式
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For n = 0 To 3
Dim ctl As New item_button
AddHandler ctl.Click, AddressOf Me.ClickMe
ctl.Name = "btn" & n
ctl.btn.Text = "Button " & n
ctl.btnID = n
ctl.Location = New Point(10, n * 50)
Me.Controls.Add(ctl)
Next
End Sub
Public Sub ClickMe(ByVal s As Object, ByVal e As EventArgs)
'do something
Dim btn As item_button
btn = CType(s, item_button)
TextBox1.Text = "Button " & s.btnID & " was pressed"
End Sub
End Class
ITEM_BUTTON
Public Class item_button
Public btnID As Integer
Public btnColor As System.Drawing.Color
Public Function ClickIt() As Integer
Return btnID
End Function
End Class