簡體   English   中英

當使用數組時,如何使textbox.text中的字符串給出整數值?

[英]How do you make string from textbox.text give an integer value, when using an array?

我使用整數數組保存值,因此該函數可以檢查該文本框中的值。 在這種情況下答案(4)。 當它等於1時,將輸出一個標簽(通過該函數后),說明輸入到文本框中的文本是true還是false。

但是,當在文本框中鍵入文本並按下按鈕時,出現錯誤,因為數組是整數,而文本框內的文本則不會,因為它是字符串。

如何使文本輸入給出整數值?

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

您可以使用以下代碼, 文本必須為truefalse (大寫或小寫都無所謂),否則會出現問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM