2019-05-30
'两个文字列表框,一个文本框,两个按钮
Dim k As Boolean
Private Sub Command1_Click() '产生一个随机数
Text1 = ""
Label1 = ""
Label2 = "请在框中输入数字,按回车键猜数"
k = False
Do While k = False
Label1 = Int(100 * Rnd()) '产生的随机数到L1
DoEvents
Loop
End Sub
Private Sub Command2_Click() '停止滚动随机数
k = True
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) '回车键事件
Static i!
If IsNumeric(Text1) = False Then '主要判断粘贴的非法数据
MsgBox "必须是数字"
ElseIf KeyCode = 13 Then
If Text1 > Label1 Then
MsgBox "您输入的数太大,请重输"
Text1 = ""
'Text1.SetFocus
ElseIf Text1 < Label1 Then
MsgBox "您输入的数太小,请重输"
Text1 = ""
'Text1.SetFocus
Else
MsgBox "您猜中了"
End If
i = i + 1
Label2 = "已猜" & i & "次" '记录次数到L2
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) '限制输入数据
If InStr("0123456789" & Chr(8), Chr(KeyAscii)) = 0 Then
MsgBox "错误,只能输入0-9的阿拉伯数字"
KeyAscii = 0
End If
End Sub
'两个文字列表框,一个文本框,两个按钮
Dim k As Boolean
Private Sub Command1_Click() '产生一个随机数
Text1 = ""
Label1 = ""
Label2 = "请在框中输入数字,按回车键猜数"
k = False
Do While k = False
Label1 = Int(100 * Rnd()) '产生的随机数到L1
DoEvents
Loop
End Sub
Private Sub Command2_Click() '停止滚动随机数
k = True
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) '回车键事件
Static i!
If IsNumeric(Text1) = False Then '主要判断粘贴的非法数据
MsgBox "必须是数字"
ElseIf KeyCode = 13 Then
If Text1 > Label1 Then
MsgBox "您输入的数太大,请重输"
Text1 = ""
'Text1.SetFocus
ElseIf Text1 < Label1 Then
MsgBox "您输入的数太小,请重输"
Text1 = ""
'Text1.SetFocus
Else
MsgBox "您猜中了"
End If
i = i + 1
Label2 = "已猜" & i & "次" '记录次数到L2
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) '限制输入数据
If InStr("0123456789" & Chr(8), Chr(KeyAscii)) = 0 Then
MsgBox "错误,只能输入0-9的阿拉伯数字"
KeyAscii = 0
End If
End Sub