Está en la página 1de 5

Cdigo fuente

Primera versin Crear una matriz de 1010 botones cuando se carga el formulario. El ancho y alto del botn se calcula a partir del rea til del formulario. Para que los botones estn en el formulario los aadimos a la coleccin de controles del formulario.
Public Class Form1 Dim botones(,) As Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "Botonera" CrearBotones() End Sub Private Sub CrearBotones() botones = New Button(9, 9) {} Dim ancho As Integer = CInt(Me.ClientSize.Width / 10) Dim alto As Integer = CInt(Me.ClientSize.Height / 10) For fila = 0 To 9 For columna = 0 To 9 Dim boton = New Button() botones(fila, columna) = boton boton.Text = fila & "," & columna boton.Width = ancho boton.Height = alto boton.Left = columna * ancho boton.Top = fila * alto Me.Controls.Add(boton) Next Next End Sub End Class

Segunda versin Creamos las constantes NUM_FILS y NUM_COLS para que sea sencillo cambiar el nmero de botones. Aadimos el evento click de los botones.
Public Class Form1 Dim botones(,) As Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "Botonera" CrearBotones() End Sub Private Sub CrearBotones() Const NUM_FILS = 10 Const NUM_COLS = 10 botones = New Button(NUM_FILS - 1, NUM_COLS - 1) {} Dim ancho As Integer = CInt(Me.ClientSize.Width / NUM_COLS) Dim alto As Integer = CInt(Me.ClientSize.Height / NUM_FILS) For fila = 0 To NUM_FILS - 1 For columna = 0 To NUM_COLS - 1 Dim boton = New Button() botones(fila, columna) = boton boton.Text = fila & "," & columna boton.Width = ancho boton.Height = alto boton.Left = columna * ancho boton.Top = fila * alto AddHandler boton.Click, AddressOf BotonClic Me.Controls.Add(boton) Next Next End Sub Sub BotonClic(ByVal sender As Object, ByVal e As EventArgs) Dim boton As Button = sender

boton.BackColor = Color.Aqua Me.Text = "Has pulsado el botn " & boton.Text End Sub End Class

Tercera versin Al acabar de cambiar el tamao del formulario recolocamos los botones. Para ello dividimos el procedimiento CrearBotones en dos: CrearBotones yColocarBotones. Las constantes NUM_FILS y NUM_COLS son ahora globales para que se puedan acceder desde los dos procedimientos. Desde los eventos del formulario al cargar (Load) y al acabar de redimensionar (ResizeEnd) llamamos al nuevo procedimiento ColocarBotones. Nos aseguramos al usar el procedimiento ColocarBotones que los botones ya han sido creados previamente en el procedimiento CrearBotones. (Que botones no seaNothing)
Public Class FormBotonera Const NUM_FILS = 10 Const NUM_COLS = 10 Dim botones(,) As Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "Botonera" CrearBotones() ColocarBotones() End Sub Private Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd ColocarBotones() End Sub

Private Sub CrearBotones() botones = New Button(NUM_FILS - 1, NUM_COLS - 1) {} For fila = 0 To NUM_FILS - 1 For columna = 0 To NUM_COLS - 1 Dim boton = New Button() botones(fila, columna) = boton boton.Text = fila & "," & columna AddHandler boton.Click, AddressOf BotonClic Me.Controls.Add(boton) Next Next End Sub Private Sub ColocarBotones() If botones Is Nothing Then Exit Sub Dim ancho As Integer = CInt(Me.ClientSize.Width / NUM_COLS) Dim alto As Integer = CInt(Me.ClientSize.Height / NUM_FILS) For fila = 0 To NUM_FILS - 1 For columna = 0 To NUM_COLS - 1 With botones(fila, columna) .Width = ancho .Height = alto .Left = columna * ancho .Top = fila * alto End With Next Next End Sub Sub BotonClic(ByVal sender As Object, ByVal e As EventArgs) Dim boton As Button = sender boton.BackColor = Color.Aqua Me.Text = "Has pulsado el botn " & boton.Text End Sub End Class

También podría gustarte