Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » [SOLVED] Creating Multiple Buttons at Runtime and Capturing Click Event


[SOLVED] Creating Multiple Buttons at Runtime and Capturing Click Event

Reply
  #1 (permalink)  
Old 11-24-06, 07:29 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
[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
__________________
Reply With Quote
  #2 (permalink)  
Old 11-25-06, 08:57 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
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
__________________
Reply With Quote
  #3 (permalink)  
Old 11-25-06, 09:07 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
I found the following way to prevent having 2 click events for the last button. I added this code replacing the old AddHandler line:

Code:
			If looper >= 1 And looper <= 19 Then
				AddHandler btn.Click, AddressOf btn_Click
			End If
This seems to fix the issue of double firing of the last click event. Is this the only solution or is there a better solution then this?
__________________
Reply With Quote
  #4 (permalink)  
Old 11-25-06, 10:52 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
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
__________________

Last edited by digioz; 02-05-07 at 09:21 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 02:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.