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

时间:2015-03-26 19:10:57

标签: vb.net winforms button dynamically-generated

这是我的代码:

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 个答案:

答案 0 :(得分:1)

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

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)

答案 1 :(得分:1)

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

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。这是一种不好的做法,因为它会在参数用法和类型的自动转换中引入细微的错误。如果您刚开始使用新项目,请记住将其属性Option Strict设置为ON