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.NET之网络编程,vb.net,button,object,string,each)