473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically create buttons

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
May 31 '07 #1
2 1904
On May 31, 11:39 am, "Lou" <lou.gar...@com cast.netwrote:
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim numberOfButtons As Integer =
CInt(Microsoft. Win32.Registry. GetValue("HKEY_ CURRENT_USER\Bu ttonDemo
\", "NumberOfButton s", 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.Contr ols.Add(b)
Next
End Sub

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

End Class

Thanks,

Seth Rowe

May 31 '07 #2
Lou
Thanks. exactly what I needed.
"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** **************@ m36g2000hse.goo glegroups.com.. .
On May 31, 11:39 am, "Lou" <lou.gar...@com cast.netwrote:
>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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim numberOfButtons As Integer =
CInt(Microsoft. Win32.Registry. GetValue("HKEY_ CURRENT_USER\Bu ttonDemo
\", "NumberOfButton s", 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.Contr ols.Add(b)
Next
End Sub

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

End Class

Thanks,

Seth Rowe

May 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13489
by: obsidian8 | last post by:
Hi All, I have looked around for an answer to this question, but haven't found one as of yet. I'm trying to use javascript to dynamically create raido buttons. I am able to create them easily enough but they aren't clickable. It doesn't matter if I set them as checked or not, you can't manipulate them at all. The code I'm using appears...
1
5638
by: David J. Berman | last post by:
Thanks for any help...! My error is: Object reference not set to an instance of an object. > public int DisplayOrder { > get { >>>>>> return (int) ViewState; > }
4
3103
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have...
2
11945
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put them each in a GroupBox. All the examples I saw from the books or from the web show me how to add static radio buttons at design, or dynamically at...
6
1484
by: Bjorn Sagbakken | last post by:
Hello In VS2005: I am adding buttons and textboxes dynamically into a table, that also dynamically expands. So far, so good, actually very nice. But I am having trouble starting the desired subroutine on postback. To me it seems like the button click event looks for a client script instead of going to the aspx codepage and execute the sub...
3
8515
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at run time and the number may vary, we can't statically tie to the event to a control. I'm assuming I need to dynamically add event handlers at run...
2
4560
by: =?Utf-8?B?TWlrZSBSYW5k?= | last post by:
I have created a method that dynamically creates a single button control (as a proof-of-concept, I would like to be able to create multiple buttons). I set properties on this control including an event handler for the click event. The problem is that when I click the button on the page and view the locals window in debug mode, I don't see any...
1
2259
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function //////////////////////////////////////////////////////////////////////////////////// version 1 : using global variables ////////////////////////////////////////////////////////////////// var _textField, _divColorPicker; // global vars...
7
1372
by: Malakie | last post by:
Hi all, I am trying to self teach VB .net to myself. Not sure what is worse, trying to accomplish this or thinking I can actually do it! ;-) Anyhow, Would someone be able to give me some example VB net code that would create buttons on a form dynamically for each drive (cd or HD or virtual etc) found on a system; labeling each created...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.