在一个类中的事件中 触发其他类的对象的事件

Public Class Form4
    Dim WithEvents t1 As New temp

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t1.init()
    End Sub

    Private Sub Form4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        AddHandler t1.MouseClick, AddressOf t1.PicMove
        t1.PicMove()
    End Sub
End Class

 

Public Class temp
    Inherits System.Windows.Forms.Form

'继承了form 为了借用form的事件
    Private WithEvents PictureboxForCustom1 As New System.Windows.Forms.PictureBox
    Function init()
        PictureboxForCustom1.BackColor = Color.Black
        PictureboxForCustom1.Location = New System.Drawing.Point(10, 10)
        PictureboxForCustom1.Size = New Size(50, 50)
        Form4.Controls.Add(PictureboxForCustom1)
        Return Nothing
    End Function

    Function PicMove()
        PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Red
        Return Nothing
    End Function

    Private Sub PictureboxForCustom1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureboxForCustom1.MouseClick
        PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Green
    End Sub

End Class

'这不是一个好的方法 但是确实触发了其他类的对象的事件

 

 

 

Public Class Form4
    Dim WithEvents t1 As New temp

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t1.init()
    End Sub

    Private Sub Form4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick, t1.CounterChanged
        AddHandler t1.CounterChanged, AddressOf t1.PicMove
        t1.PicMove()
    End Sub

End Class


Public Class temp : Inherits EventArgs

    Public Event CounterChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Private WithEvents PictureboxForCustom1 As New System.Windows.Forms.PictureBox
    Function init()
        PictureboxForCustom1.BackColor = Color.Black
        PictureboxForCustom1.Location = New System.Drawing.Point(10, 10)
        PictureboxForCustom1.Size = New Size(50, 50)
        Form4.Controls.Add(PictureboxForCustom1)
        Return Nothing
    End Function

    Private Sub PictureboxForCustom1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureboxForCustom1.MouseClick
        PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Green
    End Sub

    Function PicMove()
        PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Blue
        Return Nothing
    End Function
End Class
'在类里面添加一个自定义事件 同样可以触发

 

 

 

Public Class Form4

    Dim WithEvents t1 As New temp

    Dim t2(5) As temp

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t1.init()
        Dim t3 As New temp
        t2(1) = t3
        t2(1).init()
    End Sub

    Private Sub Form4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick, t1.CounterChanged
        AddHandler t1.CounterChanged, AddressOf t1.PicMove
        t1.PicMove()
    End Sub

End Class


Public Class temp : Inherits EventArgs

    Public Event CounterChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Private WithEvents PictureboxForCustom1 As New System.Windows.Forms.PictureBox

    Private X1 As Integer

    Function init()
        PictureboxForCustom1.BackColor = Color.Black
        PictureboxForCustom1.Location = New System.Drawing.Point(10, 10)
        PictureboxForCustom1.Size = New Size(50, 50)
        Form4.Controls.Add(PictureboxForCustom1)
        Return Nothing
    End Function

    Private Sub PictureboxForCustom1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureboxForCustom1.MouseClick
        'PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Green
    End Sub

    Function PicMove()
        PictureboxForCustom1.Location = New System.Drawing.Point(30, 30)
        PictureboxForCustom1.BackColor = Color.Blue
        Return Nothing
    End Function

End Class
'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值