2014-05-12 31 views
1

我想知道如果有一個辦法解決寫作有沒有辦法用一個類來分別處理多個事件?

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

您可以使用WithEvents這裏:

Public WithEvents image As PictureBox 

然後只有

... Handles image.MouseMove 

之後,也查看AddHandler
AddHandler可以在運行時執行什麼Handles指令在設計時執行的操作,即它使您可以將方法分配給多個/運行時創建的對象的事件。

+0

雖然有效,但它會在其他圖片框,文本框,按鈕和標籤上留下圖像的副本。同樣,當你太多地移動圖像時,它會創建3個副本,同樣也會留下副本。 (當你放手時,它們全部消失) – alexanderd5398

+1

沒關係,我修好了。 :) 謝謝你的幫助。 – alexanderd5398

+0

如果您可以共享修補程序,它可能會幫助其他人 – Mych

相關問題