Private Sub Command1_Click()
On Error Resume Next
Dim I As Integer
I = MsgBox("下面游戏开始,你准备好了吗?", vbYesNo)
If I = 6 Then Frame1.Visible = True: Text1 = "" Else Frame1.Visible = False
Text1.SetFocus
Randomize (Timer)
Text2 = Int(Rnd() * 10) + 1
Text3 = 0
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Me.Height = 5000
Me.Width = 10500
Me.BorderStyle = 1
Me.Caption = "猜数游戏"
Label1.Left = 0
Label1.Top = 1000
Label1.Caption = "欢迎进入猜数游戏"
Label1.FontName = "楷体_GB2312"
Label1.ForeColor = &H80FF&
Label1.FontSize = 48
Command1.Caption = "开始"
'Command1.Default = True
Command2.Caption = "退出"
Command2.Cancel = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 13 Then KeyAscii = 0
If KeyAscii = 13 Then
If Text1 <> "" Then Jysz
End If
End Sub
Private Function Jysz() As Boolean
Text3 = Val(Text3) + 1
If Val(Text1) = Val(Text2) Then
MsgBox "猜对了,你真棒" & Chr(13) & Chr(10) & "请猜下一个……"
Command1_Click
ElseIf Val(Text1) > Val(Text2) Then
MsgBox "猜大了。"
Else
MsgBox "猜小了。"
End If
Text1 = ""
If Val(Text3) >= 3 Then
MsgBox "呵呵,没猜对!正确的答案是" & Text2 & "。别气馁,再来!"
Command1_Click
End If
End Function
具体程序已经发到你的邮箱songxing_cool@yahoo.com.cn
更多代码请参阅我的博客http://hi.baidu.com/zgmg/blog
窗体上放command1,text1,label1,打开代码窗口,复制下列代码:Private Num As Integer
Private Sub Command1_Click() '点击猜下一个数
Call Form_Load
Text1.Text = ""
End Sub
Private Sub Form_Load()
Num = Rnd() * 100 + 1 '产生1-100随机数
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii = 13) Then
If (Not IsNumeric(Text1.Text)) Then
MsgBox "请输入数字", vbCritical
Else
If (Text1.Text >Num) Then
Label1.Caption = "您输入的数大了"
ElseIf (Text1.Text <Num) Then
Label1.Caption = "您输入的数小了"
Else
Label1.Caption = "恭喜,您猜对了!"
End If
End If
End If
End Sub
创建一个VB工程,在窗体上放置一个文本输入框text1,一个标签Label1,三个按钮,名称分别为Command1 “开始”
Command2 “暂停”
Command3 “退出”
再加入一个定时器timer1,Interval属性设置为2000ms
双击开始按钮,添加下面代码
Private Sub Command1_Click()
Dim a As Long
a = Int(Rnd * 9000000 + 1000000)
Text1 = a
Timer1.Enabled = True
End Sub
双击暂停按钮,添加下面代码
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
双击退出按钮,添加下面代码
Private Sub Command3_Click()
Call Command2_Click
End
End Sub
双击窗体,添加下面代码
Private Sub Form_Load()
Randomize Timer
End Sub
双击定时器,添加下面代码
Private Sub Timer1_Timer()
Dim a As Long
a = Int(Rnd * 9000000 + 1000000)
Text1 = a
End Sub
运行一下,一个幸运数字程序新鲜出炉
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)