是否可以在对象中声明一个带有事件的椭圆形,然后将该椭圆形用于Form1类中的事件处理程序?
Public Class MyClass
Public WithEvents Oval As New OvalShape
Public WithEvents Label As New Label
End Class
Public Class Form1
Dim MyObject As New MyClass
Private Sub Oval_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyObject.Oval.DoubleClick
''' Do something here
End Sub
事件处理程序和处理其事件的对象必须在同一类中。
因此,将事件处理程序放在MyClass
中,并使用该类的实例:
Public Class MyClass
Public WithEvents Oval As New OvalShape
Public WithEvents Label As New Label
Private Sub Oval_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Oval.DoubleClick
''' Do something here
End Sub
End Class
如果你想真正响应双击,你必须显示椭圆和标签,这样用户就可以看到并点击它。为此,你必须采取其他步骤。您的方法根本不适合实现代码告诉我们的内容。