BC30506:Handles 子句需要在包含类型或它的基类型之一中定义的 WithEvents 变量

未在 Handles 子句中提供 WithEvents 变量。 过程声明末尾的 Handles 关键字使其处理由使用 WithEvents 关键字声明的对象变量引发的事件。

错误 ID:BC30506

更正此错误

提供必要的 WithEvents 变量。

示例

在以下示例中,Visual Basic 生成编译器错误 BC30506,因为 System.Timers.Timer 实例的定义中未使用 WithEvents 关键字。

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

另请参阅