BC30506: Handles 句には、包含型またはその基本型のいずれかで定義された WithEvents 変数が必要です

Handles句にWithEvents変数を指定しませんでした。 プロシージャ宣言の最後にある Handles キーワードにより、 WithEvents キーワードを使用して宣言されたオブジェクト変数によって発生したイベントが処理されます。

エラー ID: BC30506

このエラーを解決するには

必要な WithEvents 変数を指定します。

次の例では、WithEvents キーワードがSystem.Timers.Timer インスタンスの定義で使用されていないため、Visual Basic によってコンパイラ エラー BC30506が生成されます。

Imports System.Timers

Module Module1
    Private _timer1 As New Timer() With {.Interval = 1000, .Enabled = True}

    Sub Main()
        Console.WriteLine("Press any key to start the timer...")
        Console.ReadKey()
        _timer1.Start()
        Console.ReadKey()
    End Sub

    Private Sub Timer1_Tick(sender As Object, args As EventArgs) Handles _timer1.Elapsed
        Console.WriteLine("Press any key to terminate...")
    End Sub
End Module

次の例では、 _timer1 変数が WithEvents キーワードで定義されているため、正常にコンパイルされます。

Imports System.Timers

Module Module1
    Private WithEvents _timer1 As New Timer() With {.Interval = 1000}

    Sub Main()
        Console.WriteLine("Press any key to start the timer...")
        Console.ReadKey()
        _timer1.Start()
        Console.ReadKey()
    End Sub

    Private Sub Timer1_Tick(sender As Object, args As EventArgs) Handles _timer1.Elapsed
        Console.WriteLine("Press any key to terminate...")
    End Sub

End Module

こちらも参照ください