Creating buttons at runtime, problem ......


R

Ron

I want to create 10 buttons on a form at runtime, the code below is
only creating one button labeled 1. Any idea what I am doing wrong?

Public Class Form1
Public code(10) As String
Public buttons(10) As Button
Const buttonwh As Integer = 30

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i, j, row As Integer
Dim pntcurrent As Point

For i = 0 To 10
j = i Mod 13 + 2
If i = 13 Then
row = 65
End If
pntcurrent = New Point(j + (buttonwh + 8), row)
Call createbuttons(i, pntcurrent)
Next
For k As Integer = 0 To buttons.GetUpperBound(0)
AddHandler buttons(k).Click, AddressOf Button_click
Next
End Sub
Private Sub createbuttons(ByVal i As Integer, _
ByVal pnt As Point)
buttons(i) = New Button
buttons(i).Enabled = True
buttons(i).Visible = True
buttons(i).Text = Convert.ToString(i + 1)
buttons(i).Size = New _
System.Drawing.Size(buttonwh, buttonwh)
buttons(i).Location = pnt
Me.Controls.Add(buttons(i))
End Sub
Private Sub button_click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)

End Sub

End Class
 
Ad

Advertisements

P

Patrice

Or all buttons are created but not shown properly. I would start by dumping
the location (it looks like to me you are adding a small offset where you
would want to make a multiplication ? also i will be never 13 etc...).
 
C

Chris Dunaway

I want to create 10 buttons on a form at runtime, the code below is
only creating one button labeled 1. Any idea what I am doing wrong?

Public Class Form1
Public code(10) As String
Public buttons(10) As Button
Const buttonwh As Integer = 30

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i, j, row As Integer
Dim pntcurrent As Point

For i = 0 To 10
j = i Mod 13 + 2
What is the purpose of calculating j? Since i is always less than 13,
the Mod calculation will always yield the value of i (0 Mod 13 = 0, 1
Mod 13 = 1, 2 Mod 13 = 2, etc). You can just write j = i + 2. Also,
i will never be 13 so your next line will never fall into the if
block.
If i = 13 Then
row = 65
End If
pntcurrent = New Point(j + (buttonwh + 8), row)
Call createbuttons(i, pntcurrent)
Next
End Class
It looks like 10 buttons should be created. Have you stepped through
the code to verify that it is calling the createbuttons method 10
times? By the way the Call keyword is not necessary.

Chris
 
R

Ron

I don't get 10 buttons I only get one button created with the number 1
 
Ad

Advertisements

P

Patrice

You meant you "see" one button ? IMO they are created but the code that
places them seems buggy to me. I would suggest to start with something
simple such as i*20 for both x and y cooordinates.

Then revise your code so that they are placed as you wish (looks like Mod
could do something else than you thought also you have a test on a number
that is never reached in your loop etc...)
 

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