2014-10-30 31 views
1

我正在使用整數數組來保存一個值,所以函數可以檢查該文本框中的值。在這種情況下答案(4)。當這等於1時,輸出一個標籤(在通過函數之後),說明輸入到文本框中的文本是真還是假。如何使textbox.text中的字符串在使用數組時給出整數值?

但是,當文本在文本框中鍵入並按下按鈕時,由於數組是整數,文本框內的文本不會執行操作,因爲它是字符串。

我該如何讓文本輸入給出一個整數值?

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 
+0

你可以像在你的'TextBox1_TextChanged'事件中做類似的邏輯嗎?如果'TxTq4.Text =「true」'給變量賦值1,那麼如果'answers(4)= variable',則進入條件。 – 2014-10-30 21:30:14

回答

0

您可以使用下面的代碼文本必須是真實(大寫或小寫不要緊),否則你有問題:

If answers(4) = answer(Convert.ToInt32(Convert.ToBoolean(TxTq4.Text))) Then 
相關問題