Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamically create buttons

60 views
Skip to first unread message

Lou

unread,
May 31, 2007, 11:39:40 AM5/31/07
to
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


rowe_newsgroups

unread,
May 31, 2007, 11:59:21 AM5/31/07
to

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

Lou

unread,
May 31, 2007, 1:56:10 PM5/31/07
to
Thanks. exactly what I needed.


"rowe_newsgroups" <rowe_...@yahoo.com> wrote in message
news:1180627161.3...@m36g2000hse.googlegroups.com...

0 new messages