Dynamically create buttons

L

Lou

I want to dynamically create an unknown amount of buttons into a panel.
I want each button to respond to the same click event.

In VB6 I would just create an array of buttons.
I do know how to have multiple buttons respond to the same click event in
..NET but
not if the number of buttons are not know until runtime are created
dynamically .
Example
App starts and a reg setting says create 25 buttons.

Thanks
-Lou
 
R

rowe_newsgroups

I want to dynamically create an unknown amount of buttons into a panel.
I want each button to respond to the same click event.

In VB6 I would just create an array of buttons.
I do know how to have multiple buttons respond to the same click event in
.NET but
not if the number of buttons are not know until runtime are created
dynamically .
Example
App starts and a reg setting says create 25 buttons.

Thanks
-Lou

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim numberOfButtons As Integer =
CInt(Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\ButtonDemo
\", "NumberOfButtons", 25))
For i As Integer = 0 To numberOfButtons - 1
Dim b As New Button()
AddHandler b.Click, AddressOf buttons_Click
b.Text = String.Format("Button{0}", i.ToString())
b.Top = 30 * i
Me.Panel1.Controls.Add(b)
Next
End Sub

Private Sub buttons_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
MsgBox(String.Format("{0} said Hello World!", theButton.Text))
End Sub

End Class

Thanks,

Seth Rowe
 
L

Lou

Thanks. exactly what I needed.


rowe_newsgroups said:
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim numberOfButtons As Integer =
CInt(Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\ButtonDemo
\", "NumberOfButtons", 25))
For i As Integer = 0 To numberOfButtons - 1
Dim b As New Button()
AddHandler b.Click, AddressOf buttons_Click
b.Text = String.Format("Button{0}", i.ToString())
b.Top = 30 * i
Me.Panel1.Controls.Add(b)
Next
End Sub

Private Sub buttons_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
MsgBox(String.Format("{0} said Hello World!", theButton.Text))
End Sub

End Class

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top