2012-04-05 61 views
1

我創建一個程序,它顯示在兩個單獨的標籤兩個隨機數,然後我要加/減他們,答案應該在一個文本框數字CAPTCHA驗證形成

如何添加或減去輸入數字並在文本框中輸入答案?

我在Visual Basic中創建Windows窗體它應用

+0

@ pxtoxp:見我的答案波紋管用於驗證數字驗證碼 – Suji 2014-07-29 12:56:09

回答

1

這將增加0-10之間兩個隨機數:

 Dim Rand As New System.Random 
     Dim Rand1 as integer = Rand.Next(0, 10) 
     Dim Rand2 as integer = Rand.Next(0, 10) 
     YourLabel1.Text = Rand1.toString() 
     YourLabel2.Text = Rand2.toString() 
     YourAnswerLabel.Text = Rand1 + Rand2 
+0

答案應該在一個文本框 – pxtoxp 2012-04-05 11:36:10

+3

鍵入然後到TEX鍵入答案tbox – 2012-04-05 12:31:28

+0

你能看看這個網站請:) http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/c3ec3e64-e68d-42ba-9f34-6c9cb7c64cfa 我試過他們是代碼使用但我得到這個警告信息: 「Microsoft.VisualBasic.dll中發生類型'System.InvalidCastException'的第一次機會異常」 有人可以幫助我嗎? – pxtoxp 2012-04-05 14:37:46

1

我認爲你正在嘗試做一些安全技術。我永遠不會感激你的嘗試。基於這個問題以及我所瞭解的內容,我會給出以下代碼作爲你的建議。

代碼在標籤

Dim op As Integer ' to represent the operator 
Private Sub Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Generate.Click 
    Dim Rand As New System.Random 
    op = Rand.Next(0, 3)'randomly select operator 
    lblfirst.Text = Rand.Next(0, 10)'first random number 
    lblsecond.Text = Rand.Next(0, 10)'second random number 
    If op = 0 Then ' choosing operator based on op 
     lblop.Text = "+" 
    ElseIf op = 1 Then 
     lblop.Text = "-" 
    ElseIf op = 2 Then 
     lblop.Text = "*" 
    End If 
End Sub 

繼生成隨機數和隨機運營商是複選按鈕的點擊事件,將在標籤計算表達式,並在文本框中的答案檢查:顯示信息作爲結果

Private Sub check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles check.Click 
     Dim result As Double = 0.0 
     If op = 0 Then 
      result = CDbl(lblfirst.Text) + CDbl(lblsecond.Text) 
     ElseIf op = 1 Then 
      result = CDbl(lblfirst.Text) - CDbl(lblsecond.Text) 
     ElseIf op = 2 Then 
      result = CDbl(lblfirst.Text) * CDbl(lblsecond.Text) 
     End If 
     If result = CDbl(TextBox1.Text) Then 
      MsgBox("Correct") 
     Else 
      MsgBox("Wrong") 
     End If 
    End Sub