Recordemos que la GUI debe mostrar 10 colores diferentes, lo cual puede hacerse en cajas de texto a las cuales se les ha cambiado el color de fondo por la propiedad BackColor. Al frente de cada color se puede ubicar otra caja de texto para que el usuario de la aplicación escriba el nombre del color. Para el caso del siguiente código fuente, se habilitó la misma caja que muestra el color de tal manera que el usuario escriba el nombre "encima" del color. En el color negro y otros colores oscuros, se debe habilitar el color de fuente en blanco para que al escribir el nombre, el contraste permita visualizarlo. Esto se hace mediante la propiedad ForeColor de la sección Font de dicho objeto.
Se debe ubicar una X roja frente a cada color (puede ser una etiqueta - label, en cuya propiedad Text se escriba una X mayúscula en rojo) para el caso en que se escriba erróneamente el nombre del color mostrarla. La propiedad Visible de estos objetos se configurará inicialmente en False.
Existirán dos botones (Verificar y Borrar). El código para programarlos es el que aparece a continuación de esta nota. Recordemos que las sentencias que inician con Public y Private no se deben escribir, son suministradas por el lenguaje de programación cuando en tiempo de diseño damos doble clic sobre un objeto para iniciar a programarlo.
Se debe tener en cuenta en respetar los nombres de los objetos, es decir, cambiar los que el lenguaje asigna por defecto y ubicar los que aparecen en el código fuente.
La sentencia Dim Contador As Integer, permite "separar" un espacio en memoria para tener allí un Contador que irá acumulando puntos por cada respuesta correcta. Este contador se inicializa en el evento clic del botón verificar con el valor cero.
En cada caso se evalúa si el nombre se escribió bien y siendo así se asigna un punto más al contador, si el nombre está mal escrito, se borra el contenido de la caja de texto respectiva y se muestra la X roja correspondiente para que el usuario corrija su respuesta.
En la parte final se programa el evento TextChanged de todas las cajas de texto. Este evento se genera cada vez que se escribe en una caja de texto. De ahí que aprovechemos para verificar si se escribió bien el nombre del color y entonces no mostrar las X rojas.
![]() |
| Pantallazo del programa en ejecución. El usuario escribió mal dos colores. |
--------------------------------------------------------------------------------------
Public Class frmPaleta
Dim Contador As Integer
Private Sub btnVerificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerificar.Click
varContador = 0
If txtAmarillo.Text = "AMARILLO" Then
varContador = varcontador + 1
Else
varContador = varContador
txtAmarillo.Text = ""
lblXAmarillo.Visible = True
End If
If txtAzul.Text = "AZUL" Then
varContador = varContador + 1
Else
varContador = varContador
txtAzul.Text = ""
lblXAzul.Visible = True
End If
If txtBlanco.Text = "BLANCO" Then
varContador = varContador + 1
Else
varContador = varContador
txtBlanco.Text = ""
lblXBlanco.Visible = True
End If
If txtGris.Text = "GRIS" Then
varContador = varContador + 1
Else
varContador = varContador
txtGris.Text = ""
lblXGris.Visible = True
End If
If txtMorado.Text = "MORADO" Then
varContador = varContador + 1
Else
varContador = varContador
txtMorado.Text = ""
lblXMorado.Visible = True
End If
If txtNaranja.Text = "NARANJA" Then
varContador = varContador + 1
Else
varContador = varContador
txtNaranja.Text = ""
lblXNaranja.Visible = True
End If
If txtNegro.Text = "NEGRO" Then
varContador = varContador + 1
Else
varContador = varContador
txtNegro.Text = ""
lblXNegro.Visible = True
End If
If txtRojo.Text = "ROJO" Then
varContador = varContador + 1
Else
varContador = varContador
txtRojo.Text = ""
lblXRojo.Visible = True
End If
If txtRosado.Text = "ROSADO" Then
varContador = varContador + 1
Else
varContador = varContador
txtRosado.Text = ""
lblXRosado.Visible = True
End If
If txtVerde.Text = "VERDE" Then
varContador = varContador + 1
Else
varContador = varContador
txtVerde.Text = ""
lblXVerde.Visible = True
End If
If varContador = 10 Then
MsgBox("FELICITACIONES", MsgBoxStyle.Exclamation, "PALETA DE COLORES")
Else
MsgBox("POR FAVOR REVISE...", MsgBoxStyle.Critical, "PALETA DE COLORES")
End If
lblPorcentaje.Text = varContador * 10 & "%"
lblAviso2.Visible = True
lblPorcentaje.Visible = True
End Sub
Private Sub btnBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrar.Click
txtColorAmarillo.Text = ""
lblXAmarilla.Visible = False
txtColorAzul.Text = ""
lblXAzul.Visible = False
txtColorBlanco.Text = ""
lblXBlanco.Text = ""
txtColorGris.Text = ""
lblXGris.Visible = False
txtColorMorado.Text = ""
lblXMorado.Visible = False
txtColorNaranja.Text = ""
lblXNaranja.Visible = False
txtColorNegro.Text = ""
lblXNegro.Visible = False
txtColorRojo.Text = ""
lblXNegro.Visible = False
txtColorRosado.Text = ""
lblXRosado.Visible = False
txtColorVerde.Text = ""
lblXVerde.Visible = False
End Sub
Private Sub txtColorAmarillo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorAmarillo.TextChanged
If txtColorAmarillo.Text = "AMARILLO" Then
lblXAmarilla.Visible = False
End If
End Sub
Private Sub txtColorAzul_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorAzul.TextChanged
If txtColorAzul.Text = "AZUL" Then
lblXAzul.Visible = False
End If
End Sub
Private Sub txtColorRojo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorRojo.TextChanged
If txtColorRojo.Text = "ROJO" Then
lblXRojo.Visible = False
End If
End Sub
Private Sub txtColorVerde_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorVerde.TextChanged
If txtColorVerde.Text="VERDE" THEN
lblXVerde.Visible = False
End If
End Sub
Private Sub txtColorGris_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorGris.TextChanged
If txtColorGris.Text = "GRIS" Then
lblXGris.Visible = False
End If
End Sub
Private Sub txtColorNaranja_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorNaranja.TextChanged
If txtColorNaranja.Text = "NARANJA" Then
lblXNaranja.Visible = False
End If
End Sub
Private Sub txtColorNegro_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorNegro.TextChanged
If txtColorNegro.Text = "NEGRO" Then
lblXNegro.Visible = False
End If
End Sub
Private Sub txtColorBlanco_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorBlanco.TextChanged
If txtColorBlanco.Text = "BLANCO" Then
lblXBlanco.Visible = False
End If
End Sub
Private Sub txtColorRosado_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorRosado.TextChanged
If txtColorRosado.Text = "ROSADO" Then
lblXRosado.Visible = False
End If
End Sub
Private Sub txtColorMorado_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorMorado.TextChanged
If txtColorMorado.Text = "MORADO" Then
lblXMorado.Visible = False
End If
End Sub
End Class

No hay comentarios:
Publicar un comentario