VB.NET中的事件处理

VB.NET中的事件处理,vb.net,Vb.net,在VB.NET中,为什么没有触发以下自定义事件 公共类1 公共事件按钮加载(ByVal发送方作为System.Object,ByVal e作为System.EventArgs) 受保护的子按钮2\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮2。单击 附件( 升起通风按钮LOAD(发送器,e) 端接头 末级 公共类attd 将事件c1作为新类1进行Dim 子c1_buttonload(ByVal发送方作为System.

在VB.NET中,为什么没有触发以下自定义事件

公共类1
公共事件按钮加载(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)
受保护的子按钮2\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮2。单击
附件(
升起通风按钮LOAD(发送器,e)
端接头
末级
公共类attd
将事件c1作为新类1进行Dim
子c1_buttonload(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理c1.buttonload
MsgBox(“收到的事件”)
端接头
末级

您正在使用第一个表单
classes1
来显示第二个表单
attd
,然后引发CustomEvent。然后在第二个表单中,
attd
,创建第一个表单的另一个实例,
classes1
,然后尝试将处理程序附加到该实例的事件。它们是不一样的,所以不会开火

你到底想做什么还真不清楚。如果你只是在尝试一些活动,你可以试试这样的东西

表格1

Public Class Form1
    Dim attd As Form2 = New Form2
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        attd.Show()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler attd.buttonload, AddressOf buttonLoadHandler
    End Sub

    Private Sub buttonLoadHandler(sender As Object, e As EventArgs)
        MsgBox("Event received")
    End Sub

End Class
Public Class Form1
    Dim attd As Form2 = New Form2
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        attd.Show()
        attd.showMessageBox()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class
表格2

Public Class Form2
    Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        RaiseEvent buttonload(sender, e)
    End Sub
End Class
Public Class Form2
    Public Sub showMessageBox()
        MsgBox("Hello World")
    End Sub
End Class
如果您只是想让您的第二个表单响应第一个表单按钮,请单击尝试以下操作

表格1

Public Class Form1
    Dim attd As Form2 = New Form2
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        attd.Show()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler attd.buttonload, AddressOf buttonLoadHandler
    End Sub

    Private Sub buttonLoadHandler(sender As Object, e As EventArgs)
        MsgBox("Event received")
    End Sub

End Class
Public Class Form1
    Dim attd As Form2 = New Form2
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        attd.Show()
        attd.showMessageBox()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class
表格2

Public Class Form2
    Public Event buttonload(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        RaiseEvent buttonload(sender, e)
    End Sub
End Class
Public Class Form2
    Public Sub showMessageBox()
        MsgBox("Hello World")
    End Sub
End Class

更恰当地解释你的要求。。。