如何自定义从字符串数组动态创建的按钮的点击事件?
How to customize the click event of dynamically created buttons from string array?
这是我的代码:
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 并为字符串数组中的每个项目创建按钮。没问题。
如果您在我的代码中看到“????????????”部分,我不知道如何引用动态创建的按钮的文本,以便我可以正确显示正确的 "Did you select thisButton.text"。
感谢任何帮助。
谢谢!
编辑:
根据建议更改了代码:(有效)
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
您需要在事件处理程序上放置正确的签名:
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)
要获得对单击按钮的引用,您需要使用表单引擎传递给它的两个参数声明按钮单击的事件处理程序。
Private Sub buttonClick(sender as Object, e as EventArgs)
现在,这个正确的事件处理程序接收到一个名为 sender
的参数,该参数是对单击按钮的控件引用。您可以将其转换为按钮,然后提取文本 属性
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 class 的实例),您可以使用每个动态添加的按钮的标签 属性 用于存储对 class
实例的引用
附带说明一下,您的代码在没有声明两个参数的情况下也能正常工作,因为您将 Option Strict 配置设置为 OFF。这是一种不好的做法,因为它会在您的参数使用和类型的自动转换中引入细微的错误。如果您刚刚开始一个新项目,请记住将其 属性 Option Strict 设置为 ON
这是我的代码:
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 并为字符串数组中的每个项目创建按钮。没问题。
如果您在我的代码中看到“????????????”部分,我不知道如何引用动态创建的按钮的文本,以便我可以正确显示正确的 "Did you select thisButton.text"。
感谢任何帮助。
谢谢!
编辑:
根据建议更改了代码:(有效)
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
您需要在事件处理程序上放置正确的签名:
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)
要获得对单击按钮的引用,您需要使用表单引擎传递给它的两个参数声明按钮单击的事件处理程序。
Private Sub buttonClick(sender as Object, e as EventArgs)
现在,这个正确的事件处理程序接收到一个名为 sender
的参数,该参数是对单击按钮的控件引用。您可以将其转换为按钮,然后提取文本 属性
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 class 的实例),您可以使用每个动态添加的按钮的标签 属性 用于存储对 class
实例的引用附带说明一下,您的代码在没有声明两个参数的情况下也能正常工作,因为您将 Option Strict 配置设置为 OFF。这是一种不好的做法,因为它会在您的参数使用和类型的自动转换中引入细微的错误。如果您刚刚开始一个新项目,请记住将其 属性 Option Strict 设置为 ON