AddressOf with parameter

后端 未结 8 615
误落风尘
误落风尘 2021-01-07 23:33

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 mybutt         


        
相关标签:
8条回答
  • 2021-01-08 00:31

    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.

    0 讨论(0)
  • 2021-01-08 00:35

    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.

    0 讨论(0)
提交回复
热议问题