vb.net 运行时按钮和 "AddressOf " 括号

vb.net runtime buttons and "AddressOf " parenthese

在我的系统中,我正在创建运行时按钮,我创建了一个 sub 来创建所有按钮,这对我需要的东西来说很好,但是它们都去相同的 "addressOf" 我想创建单独的处理程序,但是我目前的方法不允许任何人知道一个简单的解决方法 id 不想更改我拥有的实际结构,谢谢


抱歉不知道为什么这部分很奇怪

private Sub Button(ByVal x As Integer, ByVal y As Integer, ByVal name As String, ByVal title As String, ByVal hieght As Integer, ByVal width As Integer, ByVal buttonAddress As String)

    Dim btn As Button
    btn = New Button

    With btn
        .Location = New Point(x, y)
        .Text = title
        .Name = name
        .Width = width
        .Height = hieght
        Controls.Add(btn)

        AddHandler btn.Click, AddressOf "BtnOperation_" & buttonAddress

  End With

End Sub


Public Sub BtnOperation_AddAppointment(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = DirectCast(sender, Button)
    Dim name = btn.Name

    Select Case name

        Case "Cfind_Btn"
            'when the Cfind_btn is pressend it create a Csearch textbox at runtime 
            btn.Visible = False
            GetFormType("add_CfindOK")
            CreateTxtTypeBox(BoxType.Combo_box, "CSearch_Box")

        Case "add_CfindOK"


        Case ("Cnew_Btn")
            'open the add customer form that connects to the mysql database'
    End Select
    'fetch the btn.name'
    ' then with the name use "select case" to get appropreate action of the btn. ' 

End Sub

将处理程序传递到您的 Button 工厂方法中:

private Sub Button(ByVal x As Integer, ByVal y As Integer, ByVal name As String, ByVal title As String, ByVal hieght As Integer, ByVal width As Integer, 
    clickHandler As System.EventHandler)

    Dim btn As Button
    btn = New Button

    With btn
        .Location = New Point(x, y)
        .Text = title
        .Name = name
        .Width = width
        .Height = hieght
        Controls.Add(btn)

        AddHandler btn.Click, clickHandler     
  End With

End Sub

然后,当您调用 Button 时,使用 AddressOf 传递正确的处理程序:

Button(0,0,"MyButton".....,AddressOf BtnOperation_AddAppointment)