Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2005

Tip: Looking for answers? Try searching our database.

Dynamically adding array of buttons to form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hrvoje Vrbanc - 07 Jan 2005 12:34 GMT
Hello all,

I'm relatively inexperienced with Windows Forms programming so I have one
question: what is the best way to dynamically add e.g. 100 buttons in 10 x
10 pattern, each button with different text (numbers from 1 to 100), but
with very similar event handlers (each time a certain number is compared
with the text of the button).

I have tried basically the following:

Dim i As Integer
Dim bt(10) As Button
For i = 1 To 10
   bt(i).Size = New System.Drawing.Size(35, 25)
   bt(i).Text = i
   Me.Controls.Add(bt(i))
Next

There were some errors.

I would be grateful for any help!
Hrvoje
Ed Kaim - 08 Jan 2005 00:29 GMT
Here's some code that will set up the screen as you've desribed (without the
events hooked up):
Dim bt(10, 10) As Button

Dim x, y As Integer

For y = 0 To 9

For x = 0 To 9

bt(x, y) = New Button

bt(x, y).Text = String.Format("{0},{1}", x, y)

bt(x, y).Size = New Size(35, 25)

bt(x, y).Location = New Point(40 * x, 30 * y)

Me.Controls.Add(bt(x, y))

Next

Next

> Hello all,
>
[quoted text clipped - 18 lines]
> I would be grateful for any help!
> Hrvoje
Hrvoje Vrbanc - 08 Jan 2005 17:47 GMT
Thank you Ed, that's exactly what I needed!
Now, how to hook up the onClick event to every button?

Thanks,
Hrvoje

> Here's some code that will set up the screen as you've desribed (without the
> events hooked up):
[quoted text clipped - 42 lines]
> > I would be grateful for any help!
> > Hrvoje
Ed Kaim - 10 Jan 2005 17:52 GMT
Supposed you have an event handler like:
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

MessageBox.Show(sender.ToString())

End Sub

Then you'd want to change the setup code to something like:
Dim bt(10, 10) As Button

Dim x, y As Integer

For y = 0 To 9

For x = 0 To 9

bt(x, y) = New Button

bt(x, y).Text = String.Format("B{0},{1}", x, y)

bt(x, y).Size = New Size(35, 25)

bt(x, y).Location = New Point(40 * x, 30 * y)

AddHandler bt(x, y).Click, AddressOf Buttons_Click

Me.Controls.Add(bt(x, y))

Next

Next

(Note the AddHandler that points to the event handler for Click).

> Thank you Ed, that's exactly what I needed!
> Now, how to hook up the onClick event to every button?
[quoted text clipped - 54 lines]
>> > I would be grateful for any help!
>> > Hrvoje
Hrvoje Vrbanc - 14 Jan 2005 17:05 GMT
Ed, thank you very much!
You helped me a lot.

Hrvoje

> Supposed you have an event handler like:
> Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As
[quoted text clipped - 67 lines]
> > one
> >> > question: what is the best way to dynamically add e.g. 100 buttons in

> >> > 10
> > x
[quoted text clipped - 18 lines]
> >> > I would be grateful for any help!
> >> > Hrvoje

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.