Cómo usar Visual Basic para crear un test de opción múltiple

Cada pregunta de un test de opción múltiple consiste en la pregunta y varias respuestas, solo una correcta. Puedes crear un test en Visual Basic usando labels para preguntas, radio buttons para respuestas y botones para navegar entre preguntas. En el código, usa arrays para guardar preguntas y respuestas del usuario, y una variable contador para saber en qué pregunta está. Al final, corrige y muestra el resultado en el formulario.

Paso 1: Diseñar formulario

Nuevo proyecto Visual Basic. Añade: 2 Labels, 3 RadioButtons, 2 Buttons. Arrastra: Label1 sobre Label2 arriba. RadioButton1, 2, 3 verticales bajo labels. Button1 (izq) y Button2 (der) bajo radio buttons.

Paso 2: Declarar variables

F7 para ventana de código. Nivel de clase:

Dim questions(2, 4) As String
Dim answers(2) As String
Dim quesNum As Integer

Primera línea: array 2D. Primera dimensión: cada pregunta. Segunda: pregunta, 3 respuestas, respuesta correcta. Segunda línea: array para respuestas del usuario. Tercera: contador de pregunta actual.

Paso 3: Cargar preguntas

Private Sub GetQuestions()
    questions = New String(,) {{ _
        "How many colors are in a rainbow?", "5", "6", "7", "7"}, _
        {"Who starred in Pirates of the Caribbean?", "Johnny Depp", "John Malkovich", "John Cusack", "Johnny Depp"}, _
        {"What is the capital of Florida?", "Miami", "Tallahassee", "Jacksonville", "Tallahassee"}}
End Sub

Inicializa 3 preguntas/respuestas en array. Puedes añadir más o leer de archivo, pero cambia tamaño de arrays.

Paso 4: Corregir test

Private Sub MarkTest()
    Dim grade As Integer = 0
    For i = 0 To 2
        If answers(i) = questions(i, 4) Then grade += 1
    Next
    Label1.Text = "Test finished!"
    Label2.Text = "You scored " & grade & " out of " & answers.Length & "!"
    RadioButton1.Enabled = False
    RadioButton2.Enabled = False
    RadioButton3.Enabled = False
    Button1.Enabled = False
    Button2.Enabled = False
End Sub

Declara subrutina de corrección. Variable local para puntuación. Recorre respuestas del usuario vs correctas. Cada acierto suma 1. Muestra nota en labels y deshabilita controles.

Paso 5: Cargar formulario

Private Sub Form1_Load()
    Me.Text = "My Multiple Choice Quiz!"
    GetQuestions()
    quesNum = 1
    Label1.Text = "Question " & quesNum & " of " & answers.Length
    Label2.Text = questions(0, 0)
    Button1.Text = "Previous"
    Button2.Text = "Next"
    RadioButton1.Text = questions(0, 1)
    RadioButton2.Text = questions(0, 2)
    RadioButton3.Text = questions(0, 3)
End Sub

Título en barra. Llama GetQuestions(). Inicializa contador. Muestra nº pregunta. Pregunta 1 en Label2. Textos botones. 3 respuestas en radio buttons.

Paso 6: Botón Anterior

Private Sub Button1_Click()
    If quesNum > 1 Then
        quesNum -= 1
        Label1.Text = "Question " & quesNum & " of 3"
        Label2.Text = questions(quesNum - 1, 0)
        RadioButton1.Text = questions(quesNum - 1, 1)
        RadioButton2.Text = questions(quesNum - 1, 2)
        RadioButton3.Text = questions(quesNum - 1, 3)
        If Button2.Text = "Submit" Then Button2.Text = "Next"
    End If
End Sub

Código botón "Previous". Verifica no estar en primera. Decrementa contador. Actualiza labels y radio buttons. Si estaba en última, cambia "Submit" a "Next".

Paso 7: Botón Siguiente/Enviar

Private Sub Button2_Click()
    If RadioButton1.Checked Then answers(quesNum - 1) = RadioButton1.Text
    ElseIf RadioButton2.Checked Then answers(quesNum - 1) = RadioButton2.Text
    ElseIf RadioButton3.Checked Then answers(quesNum - 1) = RadioButton3.Text
    End If
    RadioButton1.Focus()
    If quesNum < 3 Then
        quesNum += 1
        Label1.Text = "Question " & quesNum & " of " & answers.Length
        Label2.Text = questions(quesNum - 1, 0)
        RadioButton1.Text = questions(quesNum - 1, 1)
        RadioButton2.Text = questions(quesNum - 1, 2)
        RadioButton3.Text = questions(quesNum - 1, 3)
        If quesNum = 3 Then Button2.Text = "Submit"
    Else
        MarkTest()
    End If
End Sub

Código botón "Next". Guarda respuesta seleccionada en array answers. Enfoca RadioButton1. Si no es última, incrementa contador, actualiza UI. Si pasa a última, cambia texto a "Submit". Si ya en última y pulsa "Submit", llama MarkTest().

Paso 8: Ejecutar

Guarda el programa. F5 para ejecutar.