【问题标题】:vb.net runtime buttons and "AddressOf " parenthesevb.net 运行时按钮和“地址”括号
【发布时间】:2015-03-17 11:57:18
【问题描述】:

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

【问题讨论】:

    标签: vb.net oop addressof


    【解决方案1】:

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

    【讨论】:

    • " ButtonClickedEventHandler" 似乎不起作用,我可能遗漏了什么?
    • 我已经解决了 - 见上文。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2020-08-06
    • 2021-06-13
    • 2023-03-17
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多