2个回答
展开全部
两种方法:
1. 用WithEvents声明变量,这样你就可以像对待拖上去的控件一样操作:
Public Class Form3
WithEvents btn As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
MsgBox(Now)
End Sub
End Class
2. 使用AddHandler 语句:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim btn As New Button
AddHandler btn.Click, AddressOf btn_Click
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox(Now)
End Sub
End Class
WithEvents (Visual Basic):http://msdn.microsoft.com/zh-cn/library/vstudio/aty3352y
AddHandler 语句:http://msdn.microsoft.com/zh-cn/library/vstudio/7taxzxka.aspx
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询