2

私のシステムでは、ランタイムボタンを作成しています。すべてのボタンを作成するために1つのサブを作成しましたが、これは必要なものには問題ありませんが、それらはすべて同じ「addressOf」に移動します別のハンドラーを作成したいのですが、現在の方法では許可されていません簡単な回避策を知っている人は誰でも、私が持っている実際の構造を変更したくない、ありがとう


申し訳ありませんが、なぜこの部分が奇妙になっているのかわかりません

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 に答える