是否有可能在对象VB.NET中声明一个ovalshape,withevents

时间:2015-11-15 11:46:12

标签: vb.net oop object events event-handling

是否可以在对象中声明一个ovalshape,withevents,然后在我的Form1类中使用该ovalshape作为事件处理程序?

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

1 个答案:

答案 0 :(得分:0)

事件处理程序和处理其事件的对象必须位于同一个类中。

所以把eventhandler放在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

如果您打算实际响应Double Click,则必须显示Oval和Label,以便用户可以看到它并单击它。为此,你必须采取其他步骤。您的方法根本不适合实现代码告诉我们的内容。