有没有办法用一个类分别处理多个事件?

时间:2014-05-12 21:05:54

标签: vb.net class events

我想知道是否有办法写

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub 

一遍又一遍。

这就是我的尝试:

Public Class Ship
    Public Property name As String
    Public Property image As PictureBox
    Public Property length As Integer
    Public Property direction As String
    Public Property selected As Boolean
    Public Property placed As Boolean
    Public Property location As Array

    Public Sub New(ByVal namep As String, ByVal imagep As PictureBox, ByVal lengthp As Integer, ByVal directionp As String, ByVal selectedp As Boolean, ByVal placedp As Boolean, ByVal locationp As Array)
        name = namep
        image = imagep
        length = lengthp
        direction = directionp
        selected = selectedp
        placed = placedp
        location = locationp
    End Sub

    Private Sub Ship_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.image.MouseMove
                'Events here
    End Sub

End Class

但我收到了错误:事件'图片'无法找到并且预期结束语

1 个答案:

答案 0 :(得分:1)

您可以在此处使用WithEvents

Public WithEvents image As PictureBox

然后只有

... Handles image.MouseMove

之后,也查看AddHandler AddHandler可以在运行时执行Handles指令在设计时所执行的操作,即它允许您为多个/运行时创建的对象的事件分配方法。