vb.net 以编程方式添加事件处理程序

vb.net Add an event handler programmatically

我有这段代码可以从源列表创建按钮 XML:

For Each m_node In m_nodelist
        If m_node.Name = "pos" Then
            Me.LoadPos = m_node.InnerText
        End If
        If m_node.Name = "btn" Then
            'MessageBox.Show("nodos:" & m_node.Name)
            Dim MyButton As New Button
            With MyButton
                .Name = "btn" & m_node.Attributes("index").Value
                .Top = 5
                .Left = Me.Width
                .Size = New Size(32, 32)
                .Visible = True
            End With
            AddHandler MyButton.TextChanged, AddressOf MyButton_Click
            Me.Width += 37
            Me.Controls.Add(MyButton)
        End If
    Next

我想用这个:

Friend Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
    'Write code here.
End Sub

我得到:The Handles clause requires a WithEvents variable defined in the container type or in one of its base types.

  1. 我正在寻找解决此错误的方法(已在评论中解决)。
  2. 事实是我还没有证明它是否会达到预期的最终效果,每个按钮还必须有一个动态事件处理程序。

我已经用这个最终代码解决了它:

它用于从 xml 文件映射 属性 按钮并以编程方式获取按钮... 这些按钮用于启动应用程序..

Public Sub LoadComponent()
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim n_node As String = ""
        Dim c_node As Integer = 0
        m_xmld = New XmlDocument()
        m_xmld.Load("config.xml")
        m_nodelist = m_xmld.SelectSingleNode("conf").ChildNodes
        c_node = m_nodelist.Count
        AppList.Columns.Add("index")
        AppList.Columns.Add("path")
        AppList.Columns.Add("perm")
        AppList.Columns.Add("name")
        For Each m_node In m_nodelist
            If m_node.Name = "pos" Then
                LoadPos = m_node.InnerText
            End If
            If m_node.Name = "btn" Then
                'MessageBox.Show("nodos:" & m_node.Name)
                Dim indice As String = CStr(m_node.Attributes("index").Value)
                Dim path As String = CStr(m_node.Attributes("value").Value)
                Dim perm As String = CStr(m_node.Attributes("perm").Value)
                Dim AppName As String = CStr(m_node.InnerText)

                Dim ico As Bitmap = New Bitmap(32, 32)
                If File.Exists(path & AppName) Then
                    ico = Icon.ExtractAssociatedIcon(path & AppName).ToBitmap()
                End If
                Dim MyButton As New Button
                With MyButton
                    .Name = "btn" & indice
                    .Top = 5
                    .Left = Me.Width
                    .Size = New Size(32, 32)
                    .Visible = True
                    .BackgroundImage = ico
                    .BackgroundImageLayout = ImageLayout.Stretch
                End With
                AddHandler MyButton.Click, AddressOf MyButton_Click
                Width += 37
                AppList.Rows.Add(indice, path, perm, AppName)
                Controls.Add(MyButton)
            End If
        Next
        maxSize = Me.Width
    End Sub

这是事件处理程序:

Friend Sub MyButton_Click(sender As Object, e As EventArgs)
        Dim btnIndex As Integer = CInt(Replace((sender).Name, "btn", "")) - 1
        Dim path As Object = AppList.Rows.Item(btnIndex).Item("path")
        Dim AppName As Object = AppList.Rows.Item(btnIndex).Item("name")
        Dim perm As Object = AppList.Rows.Item(btnIndex).Item("perm")
        If Convert.ToString(AppName) = "" Then
            Dim CallSettingsApp As New SettingsApp With {
                .btnId = btnIndex.ToString
            }
            CallSettingsApp.ShowDialog()
        Else
            Try
                Dim process As Process = Nothing
                Dim processStartInfo As ProcessStartInfo
                processStartInfo = New ProcessStartInfo With {
                    .FileName = path & AppName
                }
                If perm Then
                    processStartInfo.Verb = "runas"
                End If
                processStartInfo.Arguments = ""
                processStartInfo.WindowStyle = ProcessWindowStyle.Normal
                processStartInfo.UseShellExecute = True
                process = Process.Start(processStartInfo)
            Catch ex As Exception : End Try
        End If
    End Sub

要在 SettingApp 上接收 btn id,必须声明一个 public 属性

Public Class SettingsApp
    Public btnId As Integer

    Private Sub SettingsApp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MessageBox.Show("dato: " & btnId)
    End Sub

End Class