两种方法:
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
回调?回调不是必要的,而且回调可以转换成事件。
你可能需要的是触发事件:RaiseEvent
Click()
执行此语句后
你控件的Click就触发了
你可以在窗体中的
contr1_click
里写东西。
简单回调没有通用性;
有兼容性的回调很麻烦。
简单回调:
Dim
f
As
Form
Set
f
=
XXXXX
'(Your
Form)
Call
f.aa()
你应用控件数组你可以先在窗体上加一个Command
然后把他的index
属性改为0
要添加10个按钮就这样写
Private
Sub
Form_Load()
For
i
=
1
To
10
Load
Command1(i)
Command1(i).Top
=
Command1(i
-
1).Top
+
Command1(0).Height
Command1(i).Visible
=
True
Next
事件如下
Private
Sub
Command1_Click(Index
As
Integer)
MsgBox
Index
End
Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)