代码之家  ›  专栏  ›  技术社区  ›  user3029953

如何自定义字符串数组中动态创建的按钮的单击事件?

  •  2
  • user3029953  · 技术社区  · 10 年前

    这是我的代码:

    Public Class Form2
    
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        AddNewButton()
    
    End Sub
    
    Public Sub AddNewButton()
    
        Dim buttonTop As Integer = 100
    
        For Each item As String In Globals.candidates
            Dim btn As New System.Windows.Forms.Button()
            Dim Location As New Point(100, (buttonTop + 20))
            btn.Location = Location
            btn.Text = item
            btn.Width = 150
            AddHandler btn.Click, AddressOf Me.buttonClick
            Me.Controls.Add(btn)
            buttonTop += 20      
        Next
    
    End Sub
    
    Private Sub buttonClick()
    
        Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", ???????????), "Confirmation", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            MessageBox.Show("Yes pressed")
        Else
            MessageBox.Show("No pressed")
        End If
    
    End Sub
    End Class
    

    Globals.candidates是一个全局字符串数组变量,其名称为“LastName,FirstName”,当加载表单时,我调用AddNewButton()Sub,它为字符串数组中的每个项创建按钮。没问题。

    如果您在我的代码中看到“????”部分,我不知道如何引用动态创建的按钮的文本,以便正确显示正确的“您是否选择了此按钮文本”。

    感谢任何帮助。

    谢谢

    编辑:

    根据建议更改代码:(工作)

    Public Class Form2
    
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        AddNewButton()
    
    End Sub
    
    Public Sub AddNewButton()
    
        Dim buttonTop As Integer = 100
    
        For Each item As String In Globals.candidates
            Dim btn As New System.Windows.Forms.Button()
            Dim Location As New Point(100, (buttonTop + 20))
            btn.Location = Location
            btn.Text = item
            btn.Width = 150
            AddHandler btn.Click, AddressOf Me.buttonClick
            Me.Controls.Add(btn)
            buttonTop += 20      
        Next
    
    End Sub
    
    Private Sub buttonClick(sender As Object, e As EventArgs)
    
        Dim btn As Button = DirectCast(sender, System.Windows.Forms.Button)
        Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            MessageBox.Show("Yes pressed")
        Else
            MessageBox.Show("No pressed")
        End If
    
    End Sub
    
    End Class
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   Steven Doggart    10 年前

    您需要在事件处理程序上放置正确的签名:

    Private Sub buttonClick(sender As Object, e As EventArgs)
    

    然后,您可以使用 sender 对象(无论单击哪个按钮)

    Dim button As Button = DirectCast(sender, System.Windows.Forms.Button)
    Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", button.Text), "Confirmation", MessageBoxButtons.YesNo)
    
        2
  •  1
  •   Steve    10 年前

    要获得对单击按钮的引用,需要使用表单引擎传递给它的两个参数声明单击按钮的事件处理程序。

    Private Sub buttonClick(sender as Object, e as EventArgs)
    

    现在,这个正确的事件处理程序接收一个名为 sender 即单击按钮的控件引用。您可以将其转换为按钮,然后提取Text属性

    Private Sub buttonClick(sender as Object, e as EventArgs)
        Dim btn = DirectCast(sender, System.Windows.Forms.Button)
        if btn IsNot Nothing then 
            Dim result As Integer = MessageBox.Show(String.Format("Did you select {0} ?", btn.Text), "Confirmation", MessageBoxButtons.YesNo)
            If result = DialogResult.Yes Then
                MessageBox.Show("Yes pressed")
            Else
                MessageBox.Show("No pressed")
            End If
        End If
    End Sub
    

    在这种简单的情况下,只要有一个字符串数据就足够了,但是,如果需要关联一个更复杂的对象(例如Person类的实例),可以使用每个动态添加的按钮的Tag属性来存储对该类实例的引用

    作为补充说明,您的代码也可以在不声明这两个参数的情况下工作,因为您有 Option Strict 配置设置为OFF。这是一种糟糕的做法,因为它会在参数使用和类型自动转换中引入细微错误。如果您刚开始一个新项目,请记住将其属性OptionStrict设置为ON