使用AddressOf将参数传递给函数

时间:2015-02-25 02:18:30

标签: .net vb.net parameters addressof

我目前正在支持VB.NET中的应用程序,其中主窗体(Form1)在特定时间显示另一种形式(Form2)。显示Form2时,将添加一个处理程序以捕获从Form2发生的事件。

这是一个简单的例子:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete
End Sub

Private Sub CalibrationCheckComplete()
    MessageBox.Show("Form2 Event raised.")
End Sub
End Class

Public Class Form2
Public Event CalibrationCheckComplete()
Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    RaiseEvent CalibrationCheckComplete()
End Sub
End Class

此示例只是向Form2上的事件添加一个处理程序,该事件在引发事件时显示MessageBox。当引发此回调函数时,实际应用程序正在使用Form1中定义的许多变量等。

现在,我想在另一个实例上显示Form2并使用相同的回调函数并知道哪个调用它。但是我在尝试将参数传递给回调(AddressOf)函数时遇到了问题。

这是我的尝试,它有错误“'AddressOf'操作数必须是方法的名称(没有括号)。”

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete(2)
End Sub
Private Sub CalibrationCheckComplete(ByVal number As Integer)
    MessageBox.Show("Form2 Event raised. Called from number:" & number)
End Sub
End Class

Public Class Form2
Public Event CalibrationCheckComplete()
Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    RaiseEvent CalibrationCheckComplete()
End Sub
End Class

如何将参数(如整数)传递给AddressOf函数,然后确定最初调用它的函数?

1 个答案:

答案 0 :(得分:0)

您必须创建各自处理数字的不同功能

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete1
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete2
End Sub

Private Sub CalibrationCheckComplete1()
    CalibrationCheckComplete(1)
End Sub

Private Sub CalibrationCheckComplete2()
    CalibrationCheckComplete(2)
End Sub

Private Sub CalibrationCheckComplete(ByVal number As Integer)
    MessageBox.Show("Form2 Event raised. Called from number:" & number)
End Sub

但我会质疑你在做什么。如果Form2知道它是如何打开的话可能会更好。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Number = 1
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Form2.Number = 2
    Form2.Show()
    AddHandler Form2.CalibrationCheckComplete, AddressOf CalibrationCheckComplete
End Sub

Private Sub CalibrationCheckComplete()
    MessageBox.Show("Form2 Event raised. Called from number:" & Form2.Number)
End Sub