I currently am supporting an application in VB.NET in where the main form (Form1) shows another form (Form2) at a particular time. When Form2 is shown, a handler is added to capture an event that occurs from Form2.
Here's a quick example:
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
This example simply adds a handler to an event on Form2 which shows a MessageBox when the event is raised. The actual application is using many variables, etc. defined in Form1 when this callback function is raised.
Now, I would to show Form2 at another instance and utilize the same callback function and know which one called it. But I'm having issues trying to pass a parameter to the callback (AddressOf) function.
Here's my attempt which has errors "'AddressOf' operand must be the name of a method (without parentheses)."
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
How can I pass a parameter, such as an integer into the AddressOf function and then determine which function originally called it?