当使用数组时,如何使textbox.text中的字符串给出整数值?

时间:2014-10-30 21:15:39

标签: arrays vb.net

我使用整数数组来保存一个值,因此该函数可以检查该文本框中的值。在这种情况下答案(4)。当它等于1时,输出一个标签(在完成该功能之后),说明输入文本框的文本是真还是假。

但是,当在文本框中键入文本并按下按钮时,我收到错误,因为数组是整数,文本框中的文本不会,因为它是一个字符串。

如何使文本输入给出一个整数值?

Public Class Form1

    Public score As Integer
    Dim answers(10) As Integer

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxTq4.TextChanged    
        If TxTq4.Text = "true" Then
            Me.score = Me.score + 1
            answers(4) = 1
        ElseIf TxTq4.Text = "True" Then
            Me.score = Me.score + 1
            answers(4) = 1
        ElseIf TxTq4.Text = ("") Then
            Me.score = Me.score + 0
        End If    
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If answers(4) = answer(TxTq4.Text) Then
            lq4.Show()
        End If
    End Sub

    Private Function answer(ByVal val As Integer)
        Dim numberTrue As Boolean    
        If answers(4) = 1 Then
            lq4.Text = "True"
            lq4.BackColor = Color.Green
            numberTrue = True    
        Else
            lq4.Text = "False"
            numberTrue = False
            lq4.BackColor = Color.Red
        End If    
        Return numberTrue    
    End Function

End Class

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码文本必须 true false (大写或小写无关紧要)否则你有一个问题:

If answers(4) = answer( Convert.ToInt32(Convert.ToBoolean(TxTq4.Text)) ) Then