通用声明:
Private carprice(0 To 9) As Long
Private carname(0 To 9) As String
Private currentcar As Integer
-------------------------------------------------------------------------------------------------
Private Sub CM1_Click()
'游戏开始,设置“确定”按钮为可用,使文本框获得焦点
Cm2.Enabled = True
Text1.SetFocus
Randomize
currentcar = Int(10 * Rnd)' 生成0~9之间的随机数。。
Label4.Caption = carname(currentcar)
Dim inttemp As Integer
For inttemp = 0 To 9
Img(inttemp).Visible = False
Next inttemp
Img(currentcar).Visible = True
End Sub
Private Sub Cm2_Click()
Dim tempprice As Long
'如果发生输入错误则跳到错误处理段
tempprice = CLng(Text1.Text)
On Error GoTo inputerror
If tempprice < 0 Then
MsgBox ("你难道想我倒帖钱?")
ElseIf (tempprice > carprice(currentcar)) Then
MsgBox ("高了高了!`")
ElseIf (tempprice < carprice(currentcar)) Then
MsgBox "低了低了!"
Else
MsgBox "恭喜你!~答对了!~!"
Cm2.Enabled = False
End If
'清空文本档,并使其获得焦点。
Text1.SetFocus
Text1.Text = ""
Exit Sub-----------------没有出现输入错误的时候退出对象事件。
inputerror:
MsgBox "输入有错误,是能输入正整数!~!!"
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Cm3_Click()
Unload Me
End Sub
Private Sub Form_Load()
'初始化物品名字和价格
carname(0) = "俺家用来买菜的车"
carname(1) = "俺家用来旅游的车"
carname(2) = "俺家小孩开的车"
carname(3) = "俺家老婆开的车"
carname(4) = "俺家倒垃圾的车"
carname(5) = "俺家用来搬东西的车"
carname(6) = "俺家用来放啤酒的车"
carname(7) = "俺家用来出远门的车"
carname(8) = "俺家不要了的车"
carname(9) = "俺家当垃圾扔了的车"
carprice(0) = 100000
carprice(1) = 207688
carprice(2) = 88888
carprice(3) = 122588
carprice(4) = 56789
carprice(5) = 54117
carprice(6) = 150027
carprice(7) = 308888
carprice(8) = 24357
carprice(9) = 37256
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'如果文本框获得焦点时
'按下ESC键清空文本框,按下ENTER键结束输入,相当于按下"确定"按狃
If KeyAscii = vbKeyEscape Then
Text1.Text = ""
ElseIf KeyAscii = vbKeyReturn Then
Cm2_Click-------------------------调用控件事件
End If
End Sub
----------------------------------------------------------------------------------------------
知识点:
1 控件数组。
2 随机数。
3 按键的简单操作。
觉得 控件数组还是挺好用的,同样类型的控件,可以一起编辑统一的属性,其实也可以先编辑好一个,然后复制 粘贴。
关键是,在代码中可以,用FOR循环 调用 控件数组,效率就高了,嘿嘿。
是Randomize不是RANDOM,这个东西,挺实用的,明天抽时间熟悉一下这个。
Cm2_Click-------------------------调用控件事件
'如果发生输入错误则跳到错误处理段
tempprice = CLng(Text1.Text)
On Error GoTo inputerror
这就是典型的深入类型不匹配。当然这个错误是函数提示的。
我就一直纳闷呢,这个程序怎么你加个ON ERRO GOTO语句,它就能监测到出错了?原来肯定是有提示的,这里就是函数运行,如果有问题必然会有提示。