[SOLVED] Creating Multiple Buttons at Runtime and Capturing Click Event
Hello Everyone,
I am trying to create multiple buttons at runtime and capture the click event for them (for all of them). However, when I try to capture the click event, I can always only capture the click event for the last button. Anyone know of a way to work around this problem? Here is the code I am using:
Code:
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
'Dim btn1 As New Button()
Public WithEvents btn As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For looper As Integer = 1 To 20
'Dim btn As New Button
btn = New Button
btn.Height = 25 ' Set Size.
btn.Width = 150
btn.Left = 20 ' Set Location
btn.Top = 20 + (looper - 1) * (btn.Height + 4)
btn.Name = "btn" & Format(looper)
btn.Text = "Button # " & looper
Me.Controls.Add(btn) ' "pass the object, not the name"
Next
MessageBox.Show(btn.Name.ToString())
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
If btn.Name = "btn1" Then
MessageBox.Show("Button 1")
ElseIf btn.Name = "btn20" Then
MessageBox.Show("Button 20")
Else
MessageBox.Show("None of the above")
End If
End Sub
End Class
Here is an update on this problem. I got a few suggestions from the MSDN forums and they work for the most part, except that now all button click events fire once, except the last one fires twice! Here is the new code:
Code:
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Public WithEvents btn As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For looper As Integer = 1 To 20
'Dim btn As New Button
btn = New Button
AddHandler btn.Click, AddressOf btn_Click
btn.Height = 25 ' Set Size.
btn.Width = 150
btn.Left = 20 ' Set Location
btn.Top = 20 + (looper - 1) * (btn.Height + 4)
btn.Name = "btn" & Format(looper)
btn.Text = "Button # " & looper
Me.Controls.Add(btn) ' "pass the object, not the name"
Next
MessageBox.Show(btn.Name.ToString())
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
Dim btn As Button = CType(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Button 1")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Button 2")
ElseIf btn.Name = "btn3" Then
MessageBox.Show("Button 3")
ElseIf btn.Name = "btn4" Then
MessageBox.Show("Button 4")
ElseIf btn.Name = "btn5" Then
MessageBox.Show("Button 5")
ElseIf btn.Name = "btn6" Then
MessageBox.Show("Button 6")
ElseIf btn.Name = "btn7" Then
MessageBox.Show("Button 7")
ElseIf btn.Name = "btn8" Then
MessageBox.Show("Button 8")
ElseIf btn.Name = "btn9" Then
MessageBox.Show("Button 9")
ElseIf btn.Name = "btn10" Then
MessageBox.Show("Button 10")
ElseIf btn.Name = "btn11" Then
MessageBox.Show("Button 11")
ElseIf btn.Name = "btn12" Then
MessageBox.Show("Button 12")
ElseIf btn.Name = "btn13" Then
MessageBox.Show("Button 13")
ElseIf btn.Name = "btn14" Then
MessageBox.Show("Button 14")
ElseIf btn.Name = "btn15" Then
MessageBox.Show("Button 15")
ElseIf btn.Name = "btn16" Then
MessageBox.Show("Button 16")
ElseIf btn.Name = "btn17" Then
MessageBox.Show("Button 17")
ElseIf btn.Name = "btn18" Then
MessageBox.Show("Button 18")
ElseIf btn.Name = "btn19" Then
MessageBox.Show("Button 19")
ElseIf btn.Name = "btn20" Then
MessageBox.Show("Button 20")
End If
End Sub
End Class
Here is the correct and FINAL version of the code above. Special thanks to Hans Passant (MSDN Forum Moderator) for answering my question:
Code:
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim btn As Button
For looper As Integer = 1 To 20
btn = New Button
AddHandler btn.Click, AddressOf btn_Click
btn.Height = 25 ' Set Size.
btn.Width = 150
btn.Left = 20 ' Set Location
btn.Top = 20 + (looper - 1) * (btn.Height + 4)
btn.Name = "btn" & Format(looper)
btn.Text = "Button # " & looper
Me.Controls.Add(btn) ' "pass the object, not the name"
Next
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Button 1")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Button 2")
ElseIf btn.Name = "btn3" Then
MessageBox.Show("Button 3")
ElseIf btn.Name = "btn4" Then
MessageBox.Show("Button 4")
ElseIf btn.Name = "btn5" Then
MessageBox.Show("Button 5")
ElseIf btn.Name = "btn6" Then
MessageBox.Show("Button 6")
ElseIf btn.Name = "btn7" Then
MessageBox.Show("Button 7")
ElseIf btn.Name = "btn8" Then
MessageBox.Show("Button 8")
ElseIf btn.Name = "btn9" Then
MessageBox.Show("Button 9")
ElseIf btn.Name = "btn10" Then
MessageBox.Show("Button 10")
ElseIf btn.Name = "btn11" Then
MessageBox.Show("Button 11")
ElseIf btn.Name = "btn12" Then
MessageBox.Show("Button 12")
ElseIf btn.Name = "btn13" Then
MessageBox.Show("Button 13")
ElseIf btn.Name = "btn14" Then
MessageBox.Show("Button 14")
ElseIf btn.Name = "btn15" Then
MessageBox.Show("Button 15")
ElseIf btn.Name = "btn16" Then
MessageBox.Show("Button 16")
ElseIf btn.Name = "btn17" Then
MessageBox.Show("Button 17")
ElseIf btn.Name = "btn18" Then
MessageBox.Show("Button 18")
ElseIf btn.Name = "btn19" Then
MessageBox.Show("Button 19")
ElseIf btn.Name = "btn20" Then
MessageBox.Show("Button 20")
End If
'MessageBox.Show("Click Event Received for button '" & btn.Name & "'")
End Sub
End Class