AddressOf with parameter

后端 未结 8 619
误落风尘
误落风尘 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:30

    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
    

提交回复
热议问题