Public Class Form1
Dim fils As Integer = 9
Dim cols As Integer = 7
Dim arreglodebotones((cols * fils) - 1) As Button
Dim Label1 As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "-"
Label1.Location = New Point(20, 20)
Label1.Font = New Font("Arial", 20, FontStyle.Bold)
Me.Controls.Add(Label1)
For a As Integer = 1 To ((cols * fils) - 1) Step 1
arreglodebotones(a) = New Button
arreglodebotones(a).Text = a
arreglodebotones(a).Width = 70
arreglodebotones(a).Height = 60
arreglodebotones(a).Font = New Font("Arial", 20, FontStyle.Bold)
If 1 = 1 Then ' haz false para ver otro orden
arreglodebotones(a).Location = New Point(50 + (((a - 1) Mod cols) * (70 + 3)), 50 + (((a - 1) \ cols) * (60 + 3))) 'el numero constante 50 es el marco por default, el 70 corresponde al ancho de los botones, el 60 a la altura, y el numero 3 espaciado entre los botones.
Else
arreglodebotones(a).Location = New Point(50 + ((cols - 1 - ((a - 1) Mod cols)) * (70 + 3)), 50 + ((fils - 1 - ((a - 1) \ cols)) * (60 + 3)))
End If
AddHandler arreglodebotones(a).Click, AddressOf Button_Click ' Asocias el evento al método Button_Click
Me.Controls.Add(arreglodebotones(a))
Next
Me.Size = New System.Drawing.Size((cols * (70 + 3)) + 110, (fils * (60 + 3)) + 200)
If (cols * (70 + 3)) + 110 > 1000 Then
Me.Width = 1000
End If
If (fils * (60 + 3)) + 200 > 700 Then
Me.Height = 700
End If
Me.AutoScroll = True
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
escritor(Array.IndexOf(arreglodebotones, sender)) ' obteniendo el key/indice en el arreglo del obeto actual en numero.
End Sub
Private Sub escritor(escritura As Integer)
Label1.Text = escritura
End Sub
End Class