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
'