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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值