2

在我的系统中,我正在创建运行时按钮,我创建了一个子按钮来创建所有按钮,这对我需要的东西来说很好,但是它们都转到相同的“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
4

1 回答 1

3

将处理程序传递给您的 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

然后,当您调用ButtonuseAddressOf以传入正确的处理程序时:

Button(0,0,"MyButton".....,AddressOf BtnOperation_AddAppointment)
于 2015-01-19T13:14:35.387 回答