使用 Visual Studio 进行测验

Quiz using Visual Studio

我想用 visual studio 做一个关于我的小测验。我的目标是每题有 10 个问题和 4 个选择。我试过堆叠按钮,然后在不同时间更改 10 个标签,这变得有点复杂......使用数组 or/and 循环我怎样才能提高效率?这就是我的

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Form1.Visible = False
        Button2.Visible = False
        Button3.Visible = False
        Button4.Visible = False
        Button5.Visible = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If RadioButton4.Checked = True Then
            Label3.Text = "1/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen
        End If
        If Label3.Text = "1/10" Then
            Button2.Visible = True
        End If
        If RadioButton1.Checked = True Or RadioButton2.Checked = True Or RadioButton3.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
        Button2.Text = "next"
    End Sub
    Private Sub Button2_click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = "What is Luke Lopez's favorite color?"
        RadioButton4.Checked = False
        RadioButton1.Text = "Purple"
        RadioButton2.Text = "Burgundy"
        RadioButton3.Text = "Turqoise"
        RadioButton4.Text = "Brown"
        If RadioButton1.Checked = True Then
            Label3.Text = "2/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen

        End If
        If Label3.Text = "2/10" Then
            Button4.Visible = True
        End If
        If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
        Button2.Text = "check"
        Button4.Text = "next"
        If Label3.Text = "2/10" Then
            Button3.Text = "next"
            Button4.Text = "Check"
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Button3.Visible = False
        Label1.Text = "What is Luke Lopez's shoe size?"
        RadioButton1.Checked = False
        RadioButton1.Text = "7"
        RadioButton2.Text = "8.5"
        RadioButton3.Text = "7.5"
        RadioButton4.Text = "8"
        Button3.Visible = False
        If RadioButton2.Checked = True Then
            Label3.Text = "3/10"
            Label4.Text = "Nice! That was Correct!
Great job!"
            Label4.ForeColor = Color.LimeGreen

        End If
        If Label3.Text = "3/10" Then
            Button5.Visible = True
            Button4.Visible = False
            Button3.Visible = False
            Button2.Visible = False
            Button1.Visible = False
        End If
        If RadioButton1.Checked = True Or RadioButton4.Checked = True Or RadioButton3.Checked = True Then
            Label4.ForeColor = Color.DarkRed
            Label4.Text = "Oh no! that was incorrect!
Try Again!"
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    End Sub

这是我的前三个问题,所有按钮、标签和单选按钮都会检查和更改每次点击,但我需要它来提高效率,因为我不想在这上面浪费很多时间和计算机内存迷你项目。

您可以将问题、选项和答案保存到数据库中,然后将它们读取到数据表中,每次要显示问题时,只需使用数据表中的数据行即可。这意味着您无需编辑以下问题:

使用'Label.TextChanged' event处理'Label3.Text':

Private Sub Label3_TextChanged(sender As Object, e As EventArgs) Handles Label3.TextChanged
    Select Case Label3.Text
        Case "1/10"
            Button2.Visible = True
        Case "2/10"
            Button4.Visible = True
            Button3.Text = "next"
            Button4.Text = "Check"
        Case "3/10"
            ...
        ...
    End Select
End Sub

使用'If ... Else ...'语句。例如:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    '...


    If RadioButton1.Checked = True Then
        Label3.Text = "2/10"
        Label4.Text = "Nice! That was Correct! Great job!"
        Label4.ForeColor = Color.LimeGreen
    Else
        Label4.ForeColor = Color.DarkRed
        Label4.Text = "Oh no! that was incorrect! Try Again!"
    End If

    'If RadioButton3.Checked = True Or RadioButton2.Checked = True Or RadioButton4.Checked = True Then
    '    Label4.ForeColor = Color.DarkRed
    '    Label4.Text = "Oh no! that was incorrect! Try Again!"
    'End If

    '...
End Sub

在评论中,我谈到了将您的数据存储在文本文件中。这就是我的文本文件的样子。每条记录都在一行中,每个字段由竖线字符 | 分隔。我选择了一个管道,因为你的问题和答案的文本中不会有任何那个字符。

1|Largest planet?|Jupiter|Venus|Saturn|Mars|1
2|Smallest planet?|Earth|Uranus|Mercury|Neptune|3
3|Comes closest to Earth?|Mars|Venus|Mercury|Jupiter|2
4|What planet is called the red planet?|Venus|Mars|Saturn|Neptune|2
5|What planet is furthest from Earth?|Saturn|Jupiter|Uranus|Neptune|4
6|How many planets in our solar system?|7|8|9|10|2
7|How many planets between Earth and Sun?|1|2|3|4|2
8|Which planet is said to have rings?|Jupiter|Uranus|Saturn|Neptune|3

您一直在使用 classes,因为 Form 是 class。 类 可以包含属性、方法和事件等。现在到class、QuestionAnswer。这些属性非常简单。 Sub New 创建 class 的新实例并使用参数设置 class.

的所有属性

Form.Load中调用FillQuestionList。此方法使用System.IO中的File class(为此需要添加一个Imports)。 ReadAllLines returns 文件中所有行的数组。我们可以遍历这些行,然后将每一行拆分为字段。 "|" 之后的小 c 告诉编译器您打算使用 Char 而不是 String。现在我们可以调用 QuestionAnswerSub New 来提供行各部分的所有属性。然后将这个新实例添加到 QuestionList,以便我们可以在程序中使用它。

接下来,Load 事件调用 DisplayQuestion。此方法也将从 btnNext 单击事件中调用。我们从表单级变量CurrentQuestionIndex中获取我们想要显示的索引。看看 class 设置我们需要的值有多方便。循环遍历所有单选按钮,清除最后一个问题中的 Tag 并清除检查。该文件提供了正确答案的数量。这存储在单选按钮的 Tag 属性.

当用户单击 btnAnswer 时,我们会获取选择了哪个单选按钮并检查 Tag 属性 以查看答案是否正确。如果是,递增 Score 并显示分数。

btnNext.Click 在递增索引和显示下一个问题之前简单地检查我们是否已经到达列表的末尾。

Public Class QuestionGame

    Private QuestionList As New List(Of QuestionAnswer)
    Private CurrentQuestionIndex As Integer
    Private Score As Integer

    Private Sub QuestionGame_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        FillQuestionList()
        DisplayQuestion()
    End Sub

    Private Sub DisplayQuestion()
        Dim QA = QuestionList(CurrentQuestionIndex)
        lblQuestion.Text = QA.Question
        RadioButton1.Text = QA.Answer1
        RadioButton2.Text = QA.Answer2
        RadioButton3.Text = QA.Answer3
        RadioButton4.Text = QA.Answer4
        For Each rb In Controls.OfType(Of RadioButton)
            rb.Tag = ""
            rb.Checked = False
        Next
        Select Case QA.Correct
            Case 1
                RadioButton1.Tag = "Correct"
            Case 2
                RadioButton2.Tag = "Correct"
            Case 3
                RadioButton3.Tag = "Correct"
            Case 4
                RadioButton4.Tag = "Correct"
        End Select
    End Sub

    Private Sub FillQuestionList()
        Dim lines = File.ReadAllLines("C:\Users\xxxxxxxx\Questions.txt")
        For Each line In lines
            Dim parts = line.Split("|"c)
            QuestionList.Add(New QuestionAnswer(CInt(parts(0)), parts(1), parts(2), parts(3), parts(4), parts(5), CInt(parts(6))))
        Next
    End Sub

    Private Sub btnAnswer_Click(sender As Object, e As EventArgs) Handles btnAnswer.Click
        Dim rb = Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
        If rb IsNot Nothing AndAlso rb.Tag.ToString = "Correct" Then
            Score += 1
            lblScore.Text = Score.ToString
            MessageBox.Show("Correct!")
            btnAnswer.Enabled = False 'The user can't increase his score by clicking answer several times
        Else
            MessageBox.Show("Sorry, wrong answer")
        End If
        btnNext.Enabled = True
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        btnNext.Enabled = False 'user can't move to next question until he answers current question
        If CurrentQuestionIndex = QuestionList.Count - 1 Then
            MessageBox.Show("This is the last question")
        Else
            CurrentQuestionIndex += 1
            DisplayQuestion()
        End If
    End Sub
End Class

Public Class QuestionAnswer
    Public Property QuestionNumber As Integer
    Public Property Question As String
    Public Property Answer1 As String
    Public Property Answer2 As String
    Public Property Answer3 As String
    Public Property Answer4 As String
    Public Property Correct As Integer
    Public Sub New(Num As Integer, Ques As String, Ans1 As String, Ans2 As String, Ans3 As String, Ans4 As String, Cor As Integer)
        QuestionNumber = Num
        Question = Ques
        Answer1 = Ans1
        Answer2 = Ans2
        Answer3 = Ans3
        Answer4 = Ans4
        Correct = Cor
    End Sub
End Class