vb.net委托简单研究

前端之家收集整理的这篇文章主要介绍了vb.net委托简单研究前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.委托传参一
'stpe1 声明一个委托
Delegate Sub DelegateHandler(ByVal str As String)

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("button2")

' step2 New 出一个委托并指定委托方法
Dim dh As DelegateHandler = New DelegateHandler(AddressOf invokes) 'invokes为方法
' step3 调用invoke
Me.Invoke(dh,New Object() {"abc"})
End Sub

Private Sub invokes(ByVal n As String)
MsgBox(n)
End Sub


2.简单指定事件非传参
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim pb As PictureBox = New PictureBox()
pb.Image = Image.FromFile("D:/vbNet/card/card/bin/Debug/a111.bmp")
pb.Width = 71
pb.Height = 96
pb.Left = 500
pb.Top = 500

Me.Controls.Add(pb)
AddHandler pb.Click,AddressOf pb_Click

End Sub

Public Sub pb_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)
MsgBox(sender.ToString)
End Sub

3焦点控件
Public Sub pb_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)
MsgBox(Me.ActiveControl.Name)
End Sub

4.循环取动态生成控件
Dim picBox As Object

For Each picBox In Me.Controls
If TypeOf picBox Is PictureBox Then
If picBox.Name = "a111" Then
MsgBox(picBox.Name)
End If
End If
Next

5.指定事件并传参二
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim pb As PictureBox = New PictureBox()
pb.Image = Image.FromFile("D:/vbNet/card/card/bin/Debug/a111.bmp")
pb.Width = 71
pb.Height = 96
pb.Left = 500
pb.Top = 500
pb.Name = "a111"
Me.Controls.Add(pb)
AddHandler pb.Click,AddressOf pb_Click
End Sub

Public Sub pb_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Dim pb As PictureBox = sender MsgBox(pb.Name) MsgBox(pb.Left) MsgBox(pb.Top) End Sub

猜你在找的VB相关文章