Question

I want to know if there a way around writing

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

End Sub 

over and over again.

This is what I tried:

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

but I got the errors: Event 'image' cannot be found and End of statement expected

Was it helpful?

Solution

You can use WithEvents here:

Public WithEvents image As PictureBox

And then only

... Handles image.MouseMove

After that, look up AddHandler, too.
AddHandler can do at runtime what the Handles directive does at design time, i.e. it enables you to assign a method to the events of multiple / runtime-created objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top