Un control común a disponer dentro de un Form son los botones, esto se hace disponiendo objetos de la clase Button.
Confeccionar un formulario que muestre tres objetos de la clase Button, disponer como etiqueta en cada botón los valores 1,2 y 3. Cuando se presiona el botón mostrar en el título del formulario el valor de la etiqueta del botón presionado.
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Text = Button1.Text End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Text = Button2.Text End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Text = Button3.Text End Sub End Class
Para el evento Click de cada botón inicializamos la propiedad Text del Form1 con la propiedad Text del botón presionado (como la clase Form1 hereda de la clase Form luego accedemos a la propiedad Text sin anteceder nombre alguno: Text = Button1.Text ):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Text = Button1.Text End Sub
Modificar el problema anterior para que se acumulen en el título del formulario los valores de los botones presionados.
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Text = Text & Button1.Text End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Text = Text & Button2.Text End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Text = Text & Button3.Text End Sub End Class
Concatenamos el valor actual de la propiedad Text del formulario con el valor de la propiedad Text del botón respectivo con el operador &:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Text = Text & Button1.Text End Sub
Similar al problema anterior solo permitir mostrar hasta 10 caracteres en el título del formulario.
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Text.Length < 10 Then Text = Text & Button1.Text End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If Text.Length < 10 Then Text = Text & Button2.Text End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click If Text.Length < 10 Then Text = Text & Button3.Text End If End Sub End Class
Como la propiedad Text es de tipo String luego podemos acceder a la propiedad Length para conocer la cantidad de caracteres almacenados:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Text.Length < 10 Then Text = Text & Button1.Text End If End Sub
Public Class Form1 Private Sub b0_Click(sender As Object, e As EventArgs) Handles b0.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b0.Text End If End Sub Private Sub b1_Click(sender As Object, e As EventArgs) Handles b1.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b1.Text End If End Sub Private Sub b2_Click(sender As Object, e As EventArgs) Handles b2.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b2.Text End If End Sub Private Sub b3_Click(sender As Object, e As EventArgs) Handles b3.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b3.Text End If End Sub Private Sub b4_Click(sender As Object, e As EventArgs) Handles b4.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b4.Text End If End Sub Private Sub b5_Click(sender As Object, e As EventArgs) Handles b5.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b5.Text End If End Sub Private Sub b6_Click(sender As Object, e As EventArgs) Handles b6.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b6.Text End If End Sub Private Sub b7_Click(sender As Object, e As EventArgs) Handles b7.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b7.Text End If End Sub Private Sub b8_Click(sender As Object, e As EventArgs) Handles b8.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b8.Text End If End Sub Private Sub b9_Click(sender As Object, e As EventArgs) Handles b9.Click If Label1.Text.Length < 12 Then Label1.Text = Label1.Text & b9.Text End If End Sub End Class