natli natli - 1 month ago 18
Vb.net Question

AddressOf with parameter

One way or another I need to link groupID (and one other integer) to the button I am dynamically adding.. any ideas?

What I can do;

AddHandler mybutton.Click, AddressOf PrintMessage

Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Dynamic event happened!")
End Sub


What I can't do, but want to;

AddHandler mybutton.Click, AddressOf PrintMessage(groupID)

Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub

Answer

There is no way to do this with AddressOf itself. What you're looking for is a lambda expression.

AddHandler myButton.Click, Function(sender, e) PrintMessage(groupId)

Private Sub PrintMessage(ByVal groupID as Integer)
    MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
Comments