AddressOf with parameter

You can use delegate which very clear for your code follow as:

Define a delegate

Public Delegate Sub ControlClickDelegate(ByVal sender As Object, ByVal e As EventArgs)

Custom button class

Public Class CustomButton
    Inherits System.Windows.Forms.Button
#Region "property delegate"

    Private controlClickDelegate As ControlClickDelegate

    Public Property ClickHandlerDelegate As ControlClickDelegate
        Get
            Return controlClickDelegate
        End Get
        Set(ByVal Value As ControlClickDelegate)
            controlClickDelegate = Value
        End Set
    End Property

#End Region

    Public Sub RegisterEventHandler()
        AddHandler Me.Click, AddressOf OnClicking
    End Sub

    Private Sub OnClicking(ByVal sender As Object, e As System.EventArgs)
        If (Me.controlClickDelegate IsNot Nothing) Then
            Me.controlClickDelegate(sender, e)
        End If
    End Sub

End Class

MainForm

Public Class MainForm
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.CusButton1.ClickHandlerDelegate = AddressOf Me.btnClick
        Me.CusButton1.RegisterEventHandler()
    End Sub

    Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
        Me.TextBox1.Text = "Hello world"
        End Sub

End Class

You can create your own button class and add anything you want to it

Public Class MyButton
    Inherits Button

    Private _groupID As Integer
    Public Property GroupID() As Integer
        Get
            Return _groupID
        End Get
        Set(ByVal value As Integer)
            _groupID = value
        End Set
    End Property

    Private _anotherInteger As Integer
    Public Property AnotherInteger() As Integer
        Get
            Return _anotherInteger
        End Get
        Set(ByVal value As Integer)
            _anotherInteger = value
        End Set
    End Property

End Class

Since VB 2010 you can simply write

Public Class MyButton
    Inherits Button

    Public Property GroupID As Integer

    Public Property AnotherInteger As Integer
End Class

You can access the button by casting the sender

Private Sub PrintMessage(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn = DirectCast(sender, MyButton)
    MessageBox.Show( _
      String.Format("GroupID = {0}, AnotherInteger = {1}", _
                    btn.GroupID, btn.AnotherInteger))
End Sub

These new properties can even be set in the properties window (under Misc).

The controls defined in the current project automatically appear in the toolbox.


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

Use the Tag property of the button.

Button1.Tag = someObject

AddressOf gets the address of a method, and thus you cannot pass parameters to it.

Tags:

Vb.Net