如何将Click事件处理程序作为参数传递给另一个子项?

时间:2019-05-23 10:08:44

标签: vb.net events parameters

在VB.Net中,我正在编写程序,但我不知道如何将Click事件处理程序作为参数传递给另一个子,以及如何从那里调用它。我可以这样做吗?如果是,怎么办?

例如:我有以下代码。在两种形式上,我有1个ListView和1个按钮。如果按下按钮,它将调用按钮的click事件。另外,如果我在ListView中按CTRL + P,它将调用同一事件。我在两种形式上都做得到。

在form1上:

Private Sub MyPrintButton1_Click(sender As Object, e As EventArgs) Handles MyPrintButton1.Click
    ' Print MyListView1 data...
End Sub


Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
    ' CTRL+P = Print
    If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
        e.Handled = True
        MyPrintButton1_Click(sender, e)
    End If
End Sub

在form2上:

Private Sub MyPrintButton2_Click(sender As Object, e As EventArgs) Handles MyPrintButton2.Click
    ' Print MyListView2 data...
End Sub

Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
    ' CTRL+P = Print
    If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
        e.Handled = True
        MyPrintButton2_Click(sender, e)
    End If
End Sub

我只想在一个不同的(公用)文件中创建一个子,并希望通过传递变量sender&e和两个Click事件的事件处理程序来调用它。

在这种情况下,两个KeyUp事件将是相同的,只是传递的事件的名称将不同:

在form1上:

Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
    ' Handle
    AllListViews_KeyUp(sender, e, AddressOf MyPrintButton1_Click)
End Sub

在form2上:

Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
    ' Handle
    AllListViews_KeyUp(sender, e, AddressOf MyPrintButton2_Click)
End Sub

公共子将类似于这样:

Public Sub AllListViews_KeyUp(sender As Object, e As KeyEventArgs, PassedPrintButton_Click As ???? Of(????))
    ' CTRL+P = Print
    If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
        e.Handled = True
        PassedPrintButton_Click(sender2, e2 ????)
    End If
End Sub

因此:我如何需要将事件作为参数传递以及如何调用事件?当我调用PassedPrintButton_Click事件时,如何传递其自己的发送者和e参数(sender2,e2)?以及如何以及在哪里可以声明它们?)

然后,当我传递MyPrintButton1_Click和addressMyPrintButton2_Click的地址时,子程序将从哪里知道点击事件的发送者和e参数?

谢谢。

0 个答案:

没有答案
相关问题