如何将数组填充到标签输出

时间:2017-01-15 21:44:12

标签: vb.net visual-studio

我正在创建驾驶执照考试。用户输入答案,然后加载第二个表单以显示评分考试。我正在使用一个数组来保存用户的答案,名为strExamAnswer()我还有一个数组来保存名为strCorrectAnswerArray()的测试答案。我创建了一个循环来检查两者,以验证用户是否输入了正确的答案。我想添加循环数据以使用“正确”或“不正确”填充标签。我试图创建一个名为strResults()的数组,以便在循环循环时填充每个标签。我不想在20种不同的IF THEN语句中这样做。如何填充输出标签并更改为与测试答案匹配的每个输出标签?我附上了两种形式的屏幕截图。 GradedExam表单应根据用户的回答填充“正确”或“不正确”字样。

Drivers License Exam Form Graded Exam Form - Trying to populate the Results column

'Option Strict ON
 Option Strict On

Public Class DriverLicenseExam


    Dim strCorrectAnswerArray() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"} 'Array to  hold answer inputs
    Dim strExamAnswer(19) As String 'Array to hold user input answer
    Dim strResults() As String 'Array to fill output labels with "Correct" or "Incorrect"

Private Sub Get_Answer() 'Fill strExamArray with user's test answers
    strExamAnswer(0) = UCase(Me.txtAns1.Text)
    strExamAnswer(1) = UCase(Me.txtAns2.Text)
    strExamAnswer(2) = UCase(Me.txtAns3.Text)
    strExamAnswer(3) = UCase(Me.txtAns4.Text)
    strExamAnswer(4) = UCase(Me.txtAns5.Text)
    strExamAnswer(5) = UCase(Me.txtAns6.Text)
    strExamAnswer(6) = UCase(Me.txtAns7.Text)
    strExamAnswer(7) = UCase(Me.txtAns8.Text)
    strExamAnswer(8) = UCase(Me.txtAns9.Text)
    strExamAnswer(9) = UCase(Me.txtAns10.Text)
    strExamAnswer(10) = UCase(Me.txtAns11.Text)
    strExamAnswer(11) = UCase(Me.txtAns12.Text)
    strExamAnswer(12) = UCase(Me.txtAns13.Text)
    strExamAnswer(13) = UCase(Me.txtAns14.Text)
    strExamAnswer(14) = UCase(Me.txtAns15.Text)
    strExamAnswer(15) = UCase(Me.txtAns16.Text)
    strExamAnswer(16) = UCase(Me.txtAns17.Text)
    strExamAnswer(17) = UCase(Me.txtAns18.Text)
    strExamAnswer(18) = UCase(Me.txtAns19.Text)
    strExamAnswer(19) = UCase(Me.txtAns20.Text)

End Sub

Private Sub Display_Answer() 'Fill strResult 
    Dim frmGradedExam As New GradedExam

    frmGradedExam.lblResult1.Text = strResults(0)
    frmGradedExam.lblResult2.Text = strResults(1)
    frmGradedExam.lblResult3.Text = strResults(2)
    frmGradedExam.lblResult4.Text = strResults(3)
    frmGradedExam.lblResult5.Text = strResults(4)
    frmGradedExam.lblResult6.Text = strResults(5)
    frmGradedExam.lblResult7.Text = strResults(6)
    frmGradedExam.lblResult8.Text = strResults(7)
    frmGradedExam.lblResult9.Text = strResults(8)
    frmGradedExam.lblResult10.Text = strResults(9)
    frmGradedExam.lblResult11.Text = strResults(10)
    frmGradedExam.lblResult12.Text = strResults(11)
    frmGradedExam.lblResult13.Text = strResults(12)
    frmGradedExam.lblResult14.Text = strResults(13)
    frmGradedExam.lblResult15.Text = strResults(14)
    frmGradedExam.lblResult16.Text = strResults(15)
    frmGradedExam.lblResult17.Text = strResults(16)
    frmGradedExam.lblResult18.Text = strResults(17)
    frmGradedExam.lblResult19.Text = strResults(18)
    frmGradedExam.lblResult20.Text = strResults(19)

End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim frmGradedExam As New GradedExam


    Call Get_Answer()

    For i = 0 To strExamAnswer.Length
            If strExamAnswer(i) = strCorrectAnswerArray(i) Then

                strResults(i) = "Correct"
            Else
                strResults(i) = "Incorrect"
            End If

        Next

试图将每个标签放入自己的数组中。这仍然无效。程序运行但是在加载GradeExam表单时不会填充标签。表格上的每个标签仍然是空白的。

Dim frmGradedExam As New GradedExam
Dim strCorrectAnswerArray() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"} 'Array to hold answer inputs
Dim strExamAnswer(19) As String 'Array to hold user input answer
Dim strResults() As String = {frmGradedExam.lblResult1.Text, frmGradedExam.lblResult2.Text, frmGradedExam.lblResult3.Text, frmGradedExam.lblResult4.Text,
    frmGradedExam.lblResult5.Text, frmGradedExam.lblResult6.Text, frmGradedExam.lblResult7.Text, frmGradedExam.lblResult8.Text,
    frmGradedExam.lblResult9.Text, frmGradedExam.lblResult10.Text, frmGradedExam.lblResult11.Text, frmGradedExam.lblResult12.Text,
    frmGradedExam.lblResult13.Text, frmGradedExam.lblResult14.Text, frmGradedExam.lblResult15.Text, frmGradedExam.lblResult16.Text,
    frmGradedExam.lblResult17.Text, frmGradedExam.lblResult18.Text, frmGradedExam.lblResult19.Text, frmGradedExam.lblResult20.Text}

2 个答案:

答案 0 :(得分:1)

  

对不起,我不明白字符串数组和字符串之间的区别   标签数组。创建标签数组的语法是什么?

在您的声明中,从String更改为Label,然后移除.Text

所以改变这个:

Dim strResults() As String = {frmGradedExam.lblResult1.Text, frmGradedExam.lblResult2.Text, frmGradedExam.lblResult3.Text, frmGradedExam.lblResult4.Text,
    frmGradedExam.lblResult5.Text, frmGradedExam.lblResult6.Text, frmGradedExam.lblResult7.Text, frmGradedExam.lblResult8.Text,
    frmGradedExam.lblResult9.Text, frmGradedExam.lblResult10.Text, frmGradedExam.lblResult11.Text, frmGradedExam.lblResult12.Text,
    frmGradedExam.lblResult13.Text, frmGradedExam.lblResult14.Text, frmGradedExam.lblResult15.Text, frmGradedExam.lblResult16.Text,
    frmGradedExam.lblResult17.Text, frmGradedExam.lblResult18.Text, frmGradedExam.lblResult19.Text, frmGradedExam.lblResult20.Text}

对此:

Dim lblResults() As Label = {frmGradedExam.lblResult1, frmGradedExam.lblResult2, frmGradedExam.lblResult3, frmGradedExam.lblResult4,
    frmGradedExam.lblResult5, frmGradedExam.lblResult6, frmGradedExam.lblResult7, frmGradedExam.lblResult8,
    frmGradedExam.lblResult9, frmGradedExam.lblResult10, frmGradedExam.lblResult11, frmGradedExam.lblResult12,
    frmGradedExam.lblResult13, frmGradedExam.lblResult14, frmGradedExam.lblResult15, frmGradedExam.lblResult16,
    frmGradedExam.lblResult17, frmGradedExam.lblResult18, frmGradedExam.lblResult19, frmGradedExam.lblResult20}

请注意,我还将名称strResults更改为lblResults

最后,在设置标签的代码中,访问.Text属性:

        If strExamAnswer(i) = strCorrectAnswerArray(i) Then
            lblResults(i).Text = "Correct"
        Else
            lblResults(i).Text = "Incorrect"
        End If

答案 1 :(得分:0)

我真的不同意你的方法,但这是你如何使用你原来的尝试来做到这一点。请注意,在btnSubmit_Click()我们创建了一个名为&{34; frmGradedExam"的GradedExam实例,然后我们将该实例传递给Display_Answer()子网,以便我们正在使用刚刚创建的实例。因此,我们创建表单的一个实例,然后根据需要传递它:

Public Class DriverLicenseExam

    Dim strCorrectAnswerArray() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"} 'Array to  hold answer inputs
    Dim strExamAnswer(19) As String 'Array to hold user input answer
    Dim strResults(19) As String 'Array to fill output labels with "Correct" or "Incorrect"

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim frmGradedExam As New GradedExam

        Dim lblResults() As Label = {frmGradedExam.lblResult1, frmGradedExam.lblResult2, frmGradedExam.lblResult3, frmGradedExam.lblResult4,
            frmGradedExam.lblResult5, frmGradedExam.lblResult6, frmGradedExam.lblResult7, frmGradedExam.lblResult8,
            frmGradedExam.lblResult9, frmGradedExam.lblResult10, frmGradedExam.lblResult11, frmGradedExam.lblResult12,
            frmGradedExam.lblResult13, frmGradedExam.lblResult14, frmGradedExam.lblResult15, frmGradedExam.lblResult16,
            frmGradedExam.lblResult17, frmGradedExam.lblResult18, frmGradedExam.lblResult19, frmGradedExam.lblResult20}

        Call Get_Answer()

        For i = 0 To strExamAnswer.GetUpperBound(0)
            If strExamAnswer(i) = strCorrectAnswerArray(i) Then
                strResults(i) = "Correct"
            Else
                strResults(i) = "Incorrect"
            End If
        Next

        Display_Answer(frmGradedExam) ' pass the instance of GradedExam we created in this sub to Display_Answer()
        frmGradedExam.Show()
    End Sub

    Private Sub Get_Answer() 'Fill strExamArray with user's test answers
        strExamAnswer(0) = UCase(Me.txtAns1.Text)
        strExamAnswer(1) = UCase(Me.txtAns2.Text)
        strExamAnswer(2) = UCase(Me.txtAns3.Text)
        strExamAnswer(3) = UCase(Me.txtAns4.Text)
        strExamAnswer(4) = UCase(Me.txtAns5.Text)
        strExamAnswer(5) = UCase(Me.txtAns6.Text)
        strExamAnswer(6) = UCase(Me.txtAns7.Text)
        strExamAnswer(7) = UCase(Me.txtAns8.Text)
        strExamAnswer(8) = UCase(Me.txtAns9.Text)
        strExamAnswer(9) = UCase(Me.txtAns10.Text)
        strExamAnswer(10) = UCase(Me.txtAns11.Text)
        strExamAnswer(11) = UCase(Me.txtAns12.Text)
        strExamAnswer(12) = UCase(Me.txtAns13.Text)
        strExamAnswer(13) = UCase(Me.txtAns14.Text)
        strExamAnswer(14) = UCase(Me.txtAns15.Text)
        strExamAnswer(15) = UCase(Me.txtAns16.Text)
        strExamAnswer(16) = UCase(Me.txtAns17.Text)
        strExamAnswer(17) = UCase(Me.txtAns18.Text)
        strExamAnswer(18) = UCase(Me.txtAns19.Text)
        strExamAnswer(19) = UCase(Me.txtAns20.Text)
    End Sub

    Private Sub Display_Answer(ByVal frmGradedExam As GradedExam)
        ' do NOT create another instance of GradedExam in here!
        frmGradedExam.lblResult1.Text = strResults(0)
        frmGradedExam.lblResult2.Text = strResults(1)
        frmGradedExam.lblResult3.Text = strResults(2)
        frmGradedExam.lblResult4.Text = strResults(3)
        frmGradedExam.lblResult5.Text = strResults(4)
        frmGradedExam.lblResult6.Text = strResults(5)
        frmGradedExam.lblResult7.Text = strResults(6)
        frmGradedExam.lblResult8.Text = strResults(7)
        frmGradedExam.lblResult9.Text = strResults(8)
        frmGradedExam.lblResult10.Text = strResults(9)
        frmGradedExam.lblResult11.Text = strResults(10)
        frmGradedExam.lblResult12.Text = strResults(11)
        frmGradedExam.lblResult13.Text = strResults(12)
        frmGradedExam.lblResult14.Text = strResults(13)
        frmGradedExam.lblResult15.Text = strResults(14)
        frmGradedExam.lblResult16.Text = strResults(15)
        frmGradedExam.lblResult17.Text = strResults(16)
        frmGradedExam.lblResult18.Text = strResults(17)
        frmGradedExam.lblResult19.Text = strResults(18)
        frmGradedExam.lblResult20.Text = strResults(19)
    End Sub

End Class