1

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?

DataCrypt
  • 307
  • 1
  • 5
  • 15
  • I believe I've figured it out by using a lambda expression: AddHandler Form2.CalibrationCheckComplete, Sub() CalibrationCheckComplete(1) End Sub Would this be the correct way of doing this? – DataCrypt Feb 25 '15 at 02:54
  • This is why events should have an `EventHandler` signature. Yours doesn’t. The `EventHandler` passes the `sender` object, which triggered the event, into the method. – Konrad Rudolph Feb 25 '15 at 15:50

1 Answers1

1

You'll have to create different function that each handle the number

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

But I would question what you are doing. It might be better if Form2 knows how it was opened.

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
the_lotus
  • 12,668
  • 3
  • 36
  • 53