积极答复者
如何设置计数变量问题

问题
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "321" Then
MsgBox("正确继续")
End
Else
MsgBox("重新输入")
TextBox1.Text = ""
TextBox1.Focus()
End IfEnd Sub
End Class怎么设计一个计数变量来控制从新输入的次数?比如说错误5次后程序自动关闭忘各位大侠指教.
答案
-
Dim errCount AS INT
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "321" Then
MsgBox("正确继续")
End
ElseIF errCount >=5 Then
MsgBox("超过5次错误,程序将关闭")
Appliation.Exit()
End IF
errCount = errCount + 1
MsgBox("重新输入")
TextBox1.Text = ""
TextBox1.Focus()
End IfEnd Sub
VB语法不是很熟,不过应该表达出了意思
全部回复
-
Dim errCount AS INT
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "321" Then
MsgBox("正确继续")
End
ElseIF errCount >=5 Then
MsgBox("超过5次错误,程序将关闭")
Appliation.Exit()
End IF
errCount = errCount + 1
MsgBox("重新输入")
TextBox1.Text = ""
TextBox1.Focus()
End IfEnd Sub
VB语法不是很熟,不过应该表达出了意思
-
Public Class Form1
Dim intWrong As Integer = 0 '初始化计数器
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
IF intWrong = 5 Then ‘如果计数器达到5,则退出程序
MessageBox.Show("出错超过5次,程序将关闭", "Caution!")
Me.Close()
Else
IF txtInput.Text = "123" Then
MessageBox.Show(“Right”)
Else
MessageBox.Show(“Wrong”)
intWrong = intWrong + 1 ’在显示出错的同时,将计数器+1
End If
End If
End Sub
End Class
偶是菜鸟,请多多指教